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
MINGW32_NT-6.2

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
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
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:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe