G
Gabriele
Hi everybody. I have a question about floating point promotion.
Given the following code:
public static void main(String[] args) {
double b = 9.2f;
System.out.println(b);
}
could someone explain to me why the result is 9.199999809265137
instead of 9.2?
As far as i know floats should be less precise than double and indeed
java let me do implicit conversions from the former to the latter and
not the opposite. But when converting a small error is introduced even
if the converted number is easily represented with exact precision in
both formats.
I also noticed that this error is not introduced in the conversion
only when the float values are easily represented using a fixed point
notation, i.e. all integer values or values such as 9.5, 3.25, 2.75,
12.125 etc. I expect Java to use standard floating point mantissa and
exponent notation which produce precision in terms of meaningful
digits, which is different from fixed point notation typical
representation errors.
So, why is this error introduced when promoting a float value to a
higher precision double and why is this error introduced only in the
above cases?
Given the following code:
public static void main(String[] args) {
double b = 9.2f;
System.out.println(b);
}
could someone explain to me why the result is 9.199999809265137
instead of 9.2?
As far as i know floats should be less precise than double and indeed
java let me do implicit conversions from the former to the latter and
not the opposite. But when converting a small error is introduced even
if the converted number is easily represented with exact precision in
both formats.
I also noticed that this error is not introduced in the conversion
only when the float values are easily represented using a fixed point
notation, i.e. all integer values or values such as 9.5, 3.25, 2.75,
12.125 etc. I expect Java to use standard floating point mantissa and
exponent notation which produce precision in terms of meaningful
digits, which is different from fixed point notation typical
representation errors.
So, why is this error introduced when promoting a float value to a
higher precision double and why is this error introduced only in the
above cases?