I am facing a problem with the conversion of value from Double to int. I try to run the following code :
int main()
{
double val_d= 6.25e-05;
cout << (1/val_d) << endl;
unsigned int val_ui = (unsigned int ) (1/val_d);
cout << val_ui << endl;
}
conversion from double to int may remove decimal part but integer part should remain as it is ?
The output i get is : 16000 15999
so why is the o/p different here ? This is happening only on fedora. On windows and Ubuntu it works fine. ( Both output are 16000)
I tweaked the above code and got the following results :
int main()
{
double val_d= 6.25e-05;
cout << (1/val_d) << endl;
double val_intermediate = (1/val_d) ;
cout << val_intermediate << endl;
unsigned int val_ui = (unsigned int ) val_intermediate;
cout << val_ui << endl;
}
NEW OUTPUT is 16000 16000 16000
When the source text “6.25e-05” is interpreted as a decimal numeral and converted to double, it is not exactly representable, because floating-point values have limited precision, and each bit has a value that is a power of two, not a decimal digit. The IEEE 754 double-precision value that is nearest to 6.25e-5 is 6.25000000000000013010426069826053208089433610439300537109375e-05, or, in hexadecimal floating-point, 0x1.0624dd2f1a9fcp-14.
When the reciprocal of this is taken, the exact mathematical result is again not exactly representable, so it must be rounded again. The nearest double-precision value is 16000 or 0x1.f4p+13.
The C++ standard allows implementations to evaluate floating-point expressions with more precision than the nominal type requires. Some implementations use extended precision, notably Intel's 80-bit floating-point type, which has a 64-bit significand. (Regular double precision has a 53-bit significand.) In this extended precision, the reciprocal is 0xf.9fffffffffffe89p+10 or 15999.99999999999966693309261245303787291049957275390625.
Obviously, when the extended-precision result is truncated to an integer, the result is 15999.
Rounding the long-double result to double would produce 16000. (You can do this with an explicit cast to double; you do not need to assign the intermediate value to a double object.)
difference in rounding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With