I've been trying to find out the reason, but I couldn't. Can anybody help me?
Look at the following example.
float f = 125.32f; System.out.println("value of f = " + f); double d = (double) 125.32f; System.out.println("value of d = " + d);
This is the output:
value of f = 125.32 value of d = 125.31999969482422
The doubleValue() method of Java Float class returns a double value corresponding to this Float Object by widening the primitive values or in simple words by directly converting it to double via doubleValue() method .
To answer your question, you can add a float to a double and vice versa. Generally, the result will be made into a double , and you will have to cast it back to a float if that is what you want.
Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float. For example, to store the annual salary of the CEO of a company, double will be a more accurate choice.
float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision.
The value of a float
does not change when converted to a double
. There is a difference in the displayed numerals because more digits are required to distinguish a double
value from its neighbors, which is required by the Java documentation. That is the documentation for toString
, which is referred (through several links) from the documentation for println
.
The exact value for 125.32f
is 125.31999969482421875. The two neighboring float
values are 125.3199920654296875 and 125.32000732421875. Observe that 125.32 is closer to 125.31999969482421875 than to either of the neighbors. Therefore, by displaying “125.32”, Java has displayed enough digits so that conversion back from the decimal numeral to float
reproduces the value of the float
passed to println
.
The two neighboring double
values of 125.31999969482421875 are 125.3199996948242045391452847979962825775146484375 and 125.3199996948242329608547152020037174224853515625.
Observe that 125.32 is closer to the latter neighbor than to the original value (125.31999969482421875). Therefore, printing “125.32” does not contain enough digits to distinguish the original value. Java must print more digits in order to ensure that a conversion from the displayed numeral back to double
reproduces the value of the double
passed to println
.
float
into a double
, there is no loss of information. Every float
can be represented exactly as a double
.System.out.println
is the exact value for the number. An exact decimal representation could require up to about 760 decimal digits. Instead, System.out.println
prints exactly the number of decimal digits that allow to parse the decimal representation back into the original float
or double
. There are more double
s, so when printing one, System.out.println
needs to print more digits before the representation becomes unambiguous.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