PRB: Cast of Float to Long Truncates Value Unexpectedly

Last reviewed: July 24, 1997
Article ID: 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, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52
  • Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.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), refer to Knowledge Base article Q12297.


Keywords : CLngIss kbfasttip
Version : 6.0 6.0a 6.0ax 7.0 1.0 1.5 1.51
Platform : MS-DOS NT WINDOWS
Issue type : kbprb


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.

Last reviewed: July 24, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.