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 &l
Converting a floating-point value to an integral value removes the fractional part, provided the result can be represented in the integral type (i.e. the value isn't too large to fit). Inserting into a stream rounds the value, which can produce a different result.
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.