I have on question regarding double precision.When a float value is passed into double then I get some different result. For e.g.
float f= 54.23f;
double d1 = f;
System.out.println(d1);
The output is 54.22999954223633. Can someone explain the reason behind this behaviour. Is it like double defaults to 14 places of decimal precision.
Actually this is only about different visual representation or converting float / double to String. Let's take a look at internal binary representation
output
it means that
f
was converted tod1
without any distortion, significant digits are the sameThis is due to the Internal Representation.
Floating-point numbers are typically packed into a computer datum as the sign bit, the exponent field, and the significand (mantissa), from left to right.
This is called as Accuracy Problems.
The fact that floating-point numbers cannot precisely represent all real numbers, and that floating-point operations cannot precisely represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers.
It is not a problem. It is how double works. You do not have to handle it and care about it. The precision of double is enough. Think, the difference between you number and the expected result is in the 14 position after decimal point.
If you need arbitrarily good precision, use the
java.math.BigDecimal
class.Or if you still want to use double. Do like this:
Please let me know in case of any doubt.
This is because the float hides the extra decimals and double shows them. The double will represent the actual number quite precisely and shows more digits.
Try this:
System.out.println(f.doubleValue()); (need to make it a Float first ofcourse)
So as you can see, the information is there, it is just rounded. Hope this helps
double
andfloat
represent numbers in different formats.Because of this you are bound to find certain numbers that store perfectly in one format but not in the other. You happen to have found one that correctly fits in a
float
but does not fitexactly
in a `double.This problem can also show itself when two different formatters are used.
The same value is printed differently for
float
anddouble
because the Java specification requires printing as many digits as needed to distinguish the value from adjacent representable values in the same type (per my answer here, and see the linked documentation for more precision in the definition).Since
float
has fewer bits to represent values, and hence fewer values, they are spaced more widely apart, and you do not need as many digits to distinguish them. When you put the value into adouble
and print it, the Java rules require that more digits be printed so that the value is distinguished from nearbydouble
values. Theprintln
function does not know that the value originally came from afloat
and does not contain as much information as can fit into adouble
.54.23f
is exactly 54.229999542236328125 (in hexadecimal, 0x1.b1d70ap+5). Thefloat
values just below and just above this are 54.2299957275390625 (0x1.b1d708p+5) and 54.23000335693359375 (0x1.b1d70cp+5). As you can see, printing “54.229999” would distinguish the value from 54.229995… and from 54.23…. However, thedouble
values just below and just above54.23f
are 54.22999954223632101957264239899814128875732421875 and 54.22999954223633523042735760100185871124267578125. To distinguish the value, you need “54.22999954223633”.