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

Last reviewed: July 22, 1997
Article ID: 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 <stdio.h>
#include <math.h>

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


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.