does precision change slightly during casting int to double in java

  • A+
Category:Languages

I have read this - Why Are Floating Point Numbers Inaccurate?

So, sometimes precision can be changed in floating point number because of it's representation style (scientific notation with an exponent and a mantissa).

but if I cast an integer value to double, is there any chance to change the precision of double slightly in java?

I mean,

    int i = 3;     double d = (double) i;      System.out.println(d); 

the output I got 3.0 as I expected.

but, is there any chance of being precision changed like 3.000001 because of representation style of double in java?

 


Not for int to double, but you might for long to double (or int to float):

  • Not all longs greater than 2^53-1 (or less than -2^53) can be represented exactly by a double;
  • Not all ints greater than 2^24-1 (or less than -2^24) can be represented exactly by a float.

This restriction comes about because of the number of bits used to represent the mantissa (53 in doubles, 24 in floats).

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: