Web lists-archives.com

[Mingw-users] NAN handled incorrectly by printf(), ok by snprintf()

A "double" with value NaN or Infinity produces strange results when formatted by printf() or by sprintf(), but gives the expected result when formatted by snprintf().
From the program below you expect 3 times the same line:

#include <stdio.h>
#include <math.h>

int main()
  char buf[24];

  printf("%7.2f %7.2f\n",NAN, INFINITY);
  sprintf(buf,"%7.2f %7.2f",NAN,INFINITY); puts(buf);
  snprintf(buf,23,"%7.2f %7.2f",NAN,INFINITY); puts(buf);

However the output is:

   1.#R    1.#J
   1.#R    1.#J
    nan     inf

gcc version 5.3.0

best wishes, Peter

Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
MinGW-users mailing list

This list observes the Etiquette found at 
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

You may change your MinGW Account Options or unsubscribe at:
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe