PRB: Cast of Float to Long Truncates Value UnexpectedlyLast reviewed: July 24, 1997Article ID: Q48928 |
The information in this article applies to:
SYMPTOMSCasting a float variable to a long value can result in a value one less than expected.
CAUSEThis is not a problem with Microsoft C, but is a function of how floating point numbers are stored in memory. When a float or double value is converted to an integer number, the value is truncated. The value 1648 and not 1649 is printed because the float value is not stored exactly as 1649.0000. The value is stored as 1648.99999...999. When you cast the double value to a long integer, the number is truncated at the decimal point to 1648.
RESOLUTIONThe workaround for this situation is to add 0.5 to the floating-point value before converting to an integer. This ensures that the integer portion of that value has the expected magnitude before the conversion (truncation) occurs.
MORE INFORMATIONIn following code, the first printf() outputs 1648 is instead of 1649. To correct this, 0.5 is added to the double before the conversion takes place. Thus, the second printf() prints the expected result, 1649.
Sample Code
/* Compile options needed: none */ #include <stdio.h> #include <math.h> double i, j; char r[] = "16.49"; main () { i = atof(r) * 100; printf ("%ld\n", (long)i); /* prints 1648 */ printf ("%ld\n", (long)(i + 0.5)); /* prints 1649 */ }For information about errors that may occur when a long double is converted to a long (16-bit only), refer to Knowledge Base article Q12297. |
Keywords : CLngIss kbfasttip
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |