RESOLUTION
The following code demonstrates this behavior. The results
are shown for both Visual C++ 5.0, Visual C++ 6.0, Visual C++ 2005 (32-bit compiler), and Visual C++ .NET (32-bit
compiler) and Visual C++ 1.52 (16-bit compiler).
Sample Code
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
double Value;
int Decimal;
int Sign;
Value = 6.6975;
(void) printf( "1) %.7f --> %.3f --> %s\n", Value, Value,
_fcvt( Value, 3, &Decimal, &Sign ) );
Value = 6.06975;
(void) printf( "2) %.7f --> %.4f --> %s\n", Value, Value,
_fcvt( Value, 4, &Decimal, &Sign ) );
Value = 6.006975;
(void) printf( "3) %.7f --> %.5f --> %s\n", Value, Value,
_fcvt( Value, 5, &Decimal, &Sign ) );
Value = 1.2345;
(void) printf( "4) %.7f --> %.3f --> %s\n", Value, Value,
_fcvt( Value, 3, &Decimal, &Sign ) );
Value = 1.02345;
(void) printf( "5) %.7f --> %.4f --> %s\n", Value, Value,
_fcvt( Value, 4, &Decimal, &Sign ) );
Value = 1.002345;
(void) printf( "6) %.7f --> %.5f --> %s\n", Value, Value,
_fcvt( Value, 5, &Decimal, &Sign ) );
}
VC++ 1.52c (16-bit compiler) results:
1) 6.6975000 --> 6.698 --> 6698
2) 6.0697500 --> 6.0698 --> 60698
3) 6.0069750 --> 6.00698 --> 600698
4) 1.2345000 --> 1.235 --> 1235
5) 1.0234500 --> 1.0235 --> 10235
6) 1.0023450 --> 1.00235 --> 100235
VC++ 5.0 (32-bit compiler) results:
1) 6.6975000 --> 6.697 --> 6697
2) 6.0697500 --> 6.0698 --> 60698
3) 6.0069750 --> 6.00697 --> 600697
4) 1.2345000 --> 1.234 --> 1234
5) 1.0234500 --> 1.0235 --> 10235
6) 1.0023450 --> 1.00235 --> 100235
With Visual C++ 5.0, test cases 2, 5, and 6 are correct, while 1,
3, and 4 do not round as expected.
To work around this behavior, add
a very small number to the variable used. In the example above, add 1e-10 to
Value. Modify each assignment, as shown in the following example:
Value = 6.06975+1e-10;