Microsoft KB Archive/68559

{|
 * width="100%"|

PRB: Bad Code Generated for &quot;a+b
a&quot; FP Comparison Expression ==
 * }

Q68559

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50 MS-DOS | OS/2 | WINDOWS kbtool kbprb -- The information in this article applies to: - The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax - Microsoft C for OS/2, versions 6.0 and 6.0a - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0 and 1.5 -- SYMPTOMS ======== The compiler optimizes expressions of the form &quot;a+b == a&quot; if a and b are floating-point variables (double or float type). RESOLUTION ========== This is expected behavior for the compiler. The proper code is generated by compiling with the /Op option, which directs the compiler to use consistent floating-point calculations. If this is not a viable option, one of the following will work around the situation: - Enclose the expression &quot;a+b&quot; in parentheses. - Make the variables a and b integers, if applicable. - Compile with the quick compile (/qc) or fast compile (/f) options. MORE INFORMATION ================ Sample code with floating-point expressions of the form &quot;a+b == a&quot; was compiled with optimization disabled (/Od) and the first few lines of the resulting assembly listing follows: ; double a,b; ; if (a+b == a) ; *** 00000b 9b d9 ee fldz *** 00000e 9b dc 16 00 00 fcom QWORD PTR _b *** 000013 9b dd d8 fstp ST(0) *** (lines deleted) ; if ((a+b) == a) ; *** 000025 9b dd 06 00 00 fld QWORD PTR _b *** 00002a 9b dc 06 00 00 fadd QWORD PTR _a *** 00002f 9b dc 16 00 00 fcom QWORD PTR _a *** 000034 9b dd d8 fstp ST(0) *** (lines deleted) Note that the first expression gets optimized to compare variable b to zero, rather than comparing a+b to a. In the second expression, a+b is correctly compared to a. Compiling with the /Op option generates the correct code in both cases. The expression (a+b == a) might be used with floating point numbers to detect when b is negligibly small in relation to a. Additional reference words: 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00 8.00c conditional KBCategory: kbtool kbprb KBSubcategory: CodeGen

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbCVCComp