Microsoft KB Archive/131314

From BetaArchive Wiki

BUG: /Ox or /Oei Generates Incorrect Floating Point Results

Q131314

1.00 1.50 1.51 1.52 WINDOWS kbtool kbbuglist --------------------------------------------------------------------- The information in this article applies to: - The Microsoft C/C++ Compiler (CL.EXE), included with: Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 --------------------------------------------------------------------- SYMPTOMS ======== Multiple floating point calculations may generate incorrect code when compiled with either the /Ox or /Oei optimizations. For example, the sample code shown at the end of this article should print 0.498418 but with either the /Ox or /Oei optimizations enabled, it prints -0.000880. RESOLUTION ========== Use either of the following workarounds: - Disable the optimizations for the entire file or to use the optimize pragma to turn them off for just one function. For example: #pragma optimize("",off) function() #pragma optimize("",on) -or- - Simplify the calculations by breaking complex statements into multiple lines of code or by using temporary variables to hold intermediate results. STATUS ====== Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available. NOTE: this is not a problem in 32-bit versions of Microsoft Visual C++. MORE INFORMATION ================ Sample Code to Demonstrate Problem ---------------------------------- /* Compile options needed: /Oei or /Ox */ #include #include float func(float x) { float c,c1,c2,c3,c4,c5,c6,c7,c8,c9,c0,d,t,z; c=.7071068; c1=-.82215223; c2=.17087277; c3=-1.13520398; c4=1.48851587; c5=.09678418; c6=-.18628806; c7=.27886807; c8=-1.26551223; c9=1.00002368; c0=.37409196; z=c*x; if (z<0) z=-z; t=1/(1+z/2); d=t*(c1+t*c2); d=t*(c3+t*(c4+d)); d=t*(c5+t*(c6+t*(c7+d))); d=-z*z+c8+t*(c9+t*(c0+d)); d=t * (float) exp(d); if (x<0) return d/2; else return 1-d/2; } void main () { const float x=-0.00396531; float y; y = func(x); if (y>0.498417 && y<0.498419) printf("Successful: func(%f) = %f\n", x, y); else printf("Failed: func(%f) = %f, should be 0.498418\n", x, y); } Additional reference words: 1.00 1.50 8.00 8.00c 8.0 8.0c KBCategory: kbtool kbbuglist KBSubcategory: CLIss

Keywords : kb16bitonly
Issue type :
Technology :


Last Reviewed: December 22, 1999
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.