Microsoft KB Archive/48928

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base Articles  


Q48928



The information in this article applies to:

  • Microsoft C for MS-DOS, versions 6.0, 6.0a, 6.0ax
  • Microsoft C/C++ for MS-DOS
  • Microsoft Visual C++, versions 1.0, 1.5, 1.51, 1.52, 2.0, 2.1, 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition, versions 5.0, 6.0
  • Microsoft Visual C++, 32-bit Professional Edition, versions 5.0, 6.0
  • Microsoft Visual C++, 32-bit Learning Edition, version 6.0




SYMPTOMS

Casting a float variable to a long value can result in a value one less than expected.


CAUSE

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.


RESOLUTION

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.


MORE INFORMATION

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.

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), 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
Issue type : kbprb
Technology : kbVCsearch kbVC400 kbAudDeveloper kbZNotKeyword8 kbvc150 kbvc100 kbZNotKeyword3 kbVC500 kbVC600 kbVC151 kbVC200 kbVC210 kbVC32bitSearch kbVC152 kbVC500Search


Last Reviewed: May 10, 2001