Bug 25975 - Problems with -ffast-math and isnan
Summary: Problems with -ffast-math and isnan
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.0.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 50724 60237 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-01-26 14:41 UTC by M Welinder
Modified: 2014-02-17 12:45 UTC (History)
2 users (show)

See Also:
Host: 686-pc-linux-gnu
Target: 686-pc-linux-gnu
Build: 686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Output from "gcc-4.0.2 -E -ffast-math nan.c" (5.31 KB, text/plain)
2006-01-26 14:43 UTC, M Welinder
Details

Note You need to log in before you can comment on or make changes to this bug.
Description M Welinder 2006-01-26 14:41:54 UTC
The following program should print "Got a NaN":

> gcc-4.0.2 nan.c && ./a.out 
Got a NaN

...but...

> gcc-4.0.2 -ffast-math nan.c && ./a.out 
Ugh!

I don't think that is proper behaviour even for -ffast-math.

This came up because someone compiled Gnumeric with -ffast-math.  It seems
that this option is thought of as "make my program faster" with no mention
of its meaning-altering effects by lots and lots of people out there.




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

int
main (int argc, char **argv)
{
  double d;

  if (sscanf ("NaN", "%lf", &d) != 1)
    d = 0;

  if (isnan (d))
    printf ("Got a NaN\n");
  else
    printf ("Ugh!\n");

  return 0;
}
Comment 1 M Welinder 2006-01-26 14:43:22 UTC
Created attachment 10732 [details]
Output from "gcc-4.0.2 -E -ffast-math nan.c"

> gcc-4.0.2 -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: ../../gcc-4.0.2/configure --enable-languages=c,c++,f95 --prefix=/usr/local/products/gcc/4.0.2 --with-gnu-as --with-as=/usr/local/products/gcc/binutils-2.16/bin/as --with-gnu-ld --with-ld=/usr/local/products/gcc/binutils-2.16/bin/ld
Thread model: posix
gcc version 4.0.2
Comment 2 Andrew Pinski 2006-01-26 14:44:49 UTC
Please read the docs about -ffast-math.  It enables the option -ffinite-math-only which means no NaNs.
Comment 3 Andrew Pinski 2006-01-26 14:47:14 UTC
-ffast-math
Sets -fno-math-errno, -funsafe-math-optimizations, 
-fno-trapping-math, -ffinite-math-only, -fno-rounding-math, -fno-signaling-nans and fcx-limited-range.
This option causes the preprocessor macro __FAST_MATH__ to be defined.

This option should never be turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications for math functions. 


-ffinite-math-only
Allow optimizations for floating-point arithmetic that assume that arguments and results are not NaNs or +-Infs.
This option should never be turned on by any -O option since it can result in incorrect output for programs which depend on an exact implementation of IEEE or ISO rules/specifications.

The default is -fno-finite-math-only. 
Comment 4 M Welinder 2006-01-26 17:22:13 UTC
ok, thanks.  I'll prevent this by checking for __FAST_MATH__ in my code.
One could argue for a warning like

nan.c:13: warning: comparison is always false due to limited range of data type

for use of isnan and similarly (with true) for finite.
Comment 5 Richard Biener 2011-10-17 07:12:38 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 6 Paolo Carlini 2011-10-17 15:43:58 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 7 Paolo Carlini 2011-10-17 16:49:03 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 8 Paolo Carlini 2011-10-17 18:53:56 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 9 Andrew Pinski 2011-10-17 19:25:07 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 10 Jonathan Wakely 2011-10-17 21:05:02 UTC
*** Bug 50724 has been marked as a duplicate of this bug. ***
Comment 11 Richard Biener 2014-02-17 12:45:57 UTC
*** Bug 60237 has been marked as a duplicate of this bug. ***