My question is pertaining to conversion in java. I have an integer 1615021049. When in divide it by 1(which is also a float), the answer i get in float is 1.61502106E9, which when converted back to integer gives me 1615021056 which is not the original number. Kindly help. The same works properly, if i convert the variables m and b to double.
int a=1615021023;
float m=1;
float b=a/m;
int d= (int) b;
The problem is that float
's mantissa has too few bits to represent 1615021049 exactly. The latter requires 31 bits, and the former only offers 23. Hence the loss of precision.
This isn't an issue with doubles
because a double
has 52 bits of mantissa, enough to represent 1615021049.
See Wikipedia.
There are an infinity of real numbers. There are only 32 bits in a float. So obviously, not every real can be represented accurately as a float. And the float representation makes small numbers more accurate than large ones.
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