Q43217: QuickC: MATH.C Sample Program Contains Incorrect Expressions
Article: Q43217
Product(s): See article
Version(s): 2.00
Operating System(s): MS-DOS
Keyword(s): ENDUSER | s_c docerr | mspl13_c
Last Modified: 6-JUN-1989
The sample program MATH.C found in the QuickC Version 2.00 on-line
help system contains the following two statements, which do
not produce the intended results:
printf( "Mantissa: %2.2lf\tExponent: %d\n", frexp( x, &n ), n );
printf( "Fraction: %2.2lf\tInteger: %lf\n", modf( x, &y ), y );
The last parameter passed to printf() in each of these statements will
not be displayed correctly when the program is compiled under QuickC
because "n" and "y" are pushed onto the stack before they are altered
in the calls to frexp() and modf().
This example illustrates the danger of relying upon the order of
expression evaluation. When compiled under Microsoft C 5.10 with
default optimizations or optimizations disabled, these statements
function as desired; that is, n and y are altered by frexp() and
modf() before they are pushed onto the stack and displayed by
printf(). When compiled under C 5.10 with maximum optimization,
however, the statements will be pushed before being changed, just as
in QuickC.
Correcting this problem is simply a matter of ensuring that frexp()
and modf() execute prior to the use of n and y, as follows:
double r;
r = frexp( x, &n );
printf( "Mantissa: %2.2lf\tExponent: %d\n", r, n );
r = modf( x, &y );
printf( "Fraction: %2.2lf\tInteger: %lf\n", r, y );
The MATH.C program is associated with the following functions:
exp pow sqrt frexp log log10
ldexp modf ceil floor fabs fmod
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.
Copyright Microsoft Corporation 1986-2002.