Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why converting from float to double changes the value?

Tags:

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 
like image 563
arthursfreire Avatar asked Jul 06 '13 16:07

arthursfreire


People also ask

Can a float be converted to a double?

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 .

What happens when float and double is added?

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.

When should I use double instead of float?

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.

Why is double more precise than float?

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.


2 Answers

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.

like image 179
Eric Postpischil Avatar answered Oct 05 '22 23:10

Eric Postpischil


  1. When you convert a float into a double, there is no loss of information. Every float can be represented exactly as a double.
  2. On the other hand, neither decimal representation printed by 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 doubles, so when printing one, System.out.println needs to print more digits before the representation becomes unambiguous.
like image 36
Pascal Cuoq Avatar answered Oct 05 '22 23:10

Pascal Cuoq