conversion from double to unsigned int

前端 未结 3 541
无人共我
无人共我 2021-01-17 18:02

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         


        
相关标签:
3条回答
  • 2021-01-17 18:46

    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.

    0 讨论(0)
  • 2021-01-17 18:48

    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.)

    0 讨论(0)
  • 2021-01-17 19:04

    difference in rounding.

    1. (1/val_d) - double is rounded to the nearest possible number that can be represented with double precision; (ex.: 3.6999999999999999 == 3.7)
    2. (unsigned int ) (1/val_d) - when casting to int decimal part is truncated, that results on rounding down (ex.: int(3.6) == 3
    0 讨论(0)
提交回复
热议问题