Studying java, I have had an unexpected output for the following code:
int i = 1234567890;
float f = i;
int result = i - (int)f;
It's turned back as the value for the "result" variable the value -46, and I was expecting 0.0, believing that "float f = i" would be converted to 1234567890.0 instead of 1.23456794E9. Trying to understand it, I have noted that for the float type is only permitted, at most, 7 digits before the radix point, and, if is imputed a float with more that 7 digits before the radix point, compilers output a string that is composed with the radix point at the 2nd index place of that string and, at the penultimate index place, its returns a "E" or "e". What is it? What "e" means?
That's scientific notation, in E notation. The E
stands for exponent.
1.23456794E9
means 1.23456794 × 109 which is the same as 1234567940.
The limited precision of the float
type means that the value is not the exact same value as the int
. The scientific notation is used to show large or small values, it doesn't have anything to do with the loss of precision.
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