Consider the following program: //---------------------------------------------------------------------- #include <iostream> #include <cmath>
int main() { for(double p = .25; p <= .5; p += .25) for(double x = -10.0; x <= 10.0; x += 20.0) { std::cout << "\nx*x = " << x*x << "\npow(100, " << p << ") = " << std::pow(100.0, p) << "\npow(x*x, " << p << ") = " << std::pow(x*x, p) << "\n"; } } //---------------------------------------------------------------------- When compiling without -ffast-math, the output is the expected one. However, if compiled with -ffast-math (other optimization options don't seem to matter), it produces incorrectly "nan" and "-10" for the third line ("std::pow(x*x, p") when 'x' is negative. If the -fno-unsafe-math-optimizations option is specified in addition to -ffast-math, the bug is not triggered. If "double xx = x*x;" is added inside the loop and then 'xx' is used instead of 'x*x', the bug is not triggered either. -- Summary: Erroneous optimization of pow() with -ffast-math Product: gcc Version: 4.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: warp at iki dot fi GCC build triplet: i586-suse-linux GCC host triplet: i586-suse-linux GCC target triplet: i586-suse-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41094