Here's what I'm looking at:
float p=1.15f;
BigDecimal bdp=new BigDecimal(p);
float q=1.1499999f;
float r=1.14999999f;
System.out.println(p); //1.15
System.out.println(bdp); //1.14999997615814208984375
System.out.println(q); //1.1499999
System.out.println(r); //1.15
So I understand that the decimal value of "p" 1.15 can't be represented exactly in binary.
And so the large big decimal "bdp" output makes perfect sense to me ... that's the actual value of the float.
Question 1
When the float "p" gets converted back to a string for output (as 1.15), how/where does that rounding occur (from the internal 1.149..375 value to 1.15)?
And where is it specified in the documentation? The toString javadoc doesn't really help (me at least).
I do see this in the language spec:
The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, respectively.
Wikipedia's IEEE 754 article gives this:
This gives from 6 to 9 significant decimal digits precision (if a decimal string with at most 6 significant decimal is converted to IEEE 754 single precision and then converted back to the same number of significant decimal, then the final string should match the original;
Question 2
So it seems that this is just how Java/IEEE 754 floats are supposed to work?
I get guaranteed accuracy of float/string conversion/representation up to a certain number of digits (like for "p" and "q"), and if that number of digits is exceeded Java will do some rounding for display (like for "r")?
Thanks for help.
System.out.println(new BigDecimal(someFloat));
to get the exact decimal representation ofsomeFloat
. – Patricia Shanahan