PRB: Cast of Float to Long Truncates Value UnexpectedlyID: Q48928
|
Casting a float variable to a long value can result in a value one less than expected.
This 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.
The 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.
In 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.
/* 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), please see the following
article in the Microsoft Knowledge Base:
Q12297 PRB: Rounding Error Casting Double to Long
Additional query words:
Keywords : kbLangC kbVC150 kbVC151 kbVC152 kbVC200 kbVC210 kbVC410 kbVC500 kbVC600
Version : MS-DOS:; WINDOWS:1.0,1.5,1.51,1.52; winnt:2.0,2.1,4.0,5.0,6.0
Platform : MS-DOS WINDOWS winnt
Issue type : kbprb
Last Reviewed: July 20, 1999