Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion from double to unsigned int

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

like image 466
Dexter Avatar asked Sep 06 '12 11:09

Dexter


2 Answers

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

like image 60
Eric Postpischil Avatar answered Oct 07 '22 22:10

Eric Postpischil


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
like image 28
spin_eight Avatar answered Oct 07 '22 23:10

spin_eight