A float is a 32 bit IEEE 754 floating point.
A double is a 64 bit IEEE 754 floating point.
so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Answer from Ross Larson on Stack OverflowA float is a 32 bit IEEE 754 floating point.
A double is a 64 bit IEEE 754 floating point.
so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Can you explain what makes this difference between float and double?
Sure. Imagine you had two decimal types, one with five significant digits, and one with ten.
What value would you use to represent pi for each of those types? In both cases you'd be trying to get as close to a number which you couldn't represent exactly - but you wouldn't end up with the same value, would you?
It's the same for float and double - both are binary floating point types, but double has more precision than float.
Videos
A float is a 32 bit IEEE 754 floating point.
A double is a 64 bit IEEE 754 floating point.
so it is just a matter of precision because neither of the fraction portions .8 and .65 have a terminating binary representation, so there is some rounding error. the double has more precision so it has slightly less rounding error.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Answer from Ross Larson on Stack OverflowPrior to Java 1.5 auto-boxing (and unboxing) didn't exist in Java. So, you would need this to extract the underlying primitive from a Double.
If you are unfamiliar with auto-boxing, you can read more here. http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html
You call 'doubleValue' on a Double object to convert from the boxed Object to the primitive data type. Since most of the time the Double is auto-unboxed to a double, you usually don't need to do this, but if you want to be explicit in your conversion, you can call this method.
New to programming and curious: what's the main difference between float and double? When should I use one over the other?
Thanks! 🙏😊