4

I used these statements to test

float f=4.35f;
int i=(int)(f*100);
System.out.println(i);
double d=4.35;
i=(int)(d*100);
System.out.println(i);

the result is

 435
 434

I used to think the only difference between float and double is just the precision. They should be the same in an calculation. But I converted 4.35 to binary and then converted it back to decimal and found it is in fact 4.3499999... So if I multiply it by 100 and then cast, I think the answer should be 434 with both float and double. Why the first one is 435?

10

2 Answers 2

6

Floating-point in Java uses a binary representation. Many numbers that have a short exact representation in decimal, such as 4.35, do not have an exact representation in binary at any precision (be it float, double or another one).

When you write 4.35 in a Java program, it is interpreted as meaning the double nearest 435/100. When you write 4.35f, it is interpreted as meaning the float nearest 435/100.

It so happens that both 4.35 and 4.35f are slightly below 435/100: in Java, 4.35 represents 4.3499999999999996447286321199499070644378662109375 exactly and 4.35f represents 4.349999904632568359375. In a first approximation, it is just chance that in both types the nearest representable value is below the target 435/100 (although when looking deeper into it, there is nothing random about it).

The number 435 on the other hand is representable exactly as both a float and a double.

When multiplying 4.35f or 4.35 by 100 (which is exactly representable as float and as double too), one of two possibilities happens:

  • the mathematical result of the multiplication is closer to 435 than to any other floating-point number. Then 435 is chosen as the result of the operation. You have to remember that Java does not know that it is multiplying a number intended to be 4.35. As far as it knows, you chose a number lower than 4.35 on purpose. Perhaps you really intended the operand to be 4.349999904632568359375. It is the consequence of a second approximation during the multiplication that the end result is 435. Anyway, converting the floating-point number 435 to int produces the int 435. This is what happens in the case of float.

  • the mathematical result of the multiplication is closest to a floating-point number below 435. In this case, that floating-point number is chosen as the result of the floating-point multiplication. Converting this result to int produces 434, because the conversion from floating-point to integer works by truncation. This is what happens in the case of double. Multiplying 4.3499999999999996447286321199499070644378662109375 by 100 produces a mathematical result that is close to the double immediately below 435.0 (this double is 434.99999999999994315658113919198513031005859375), and thus this double is used as the result of the floating-point multiplication.

0

In addition to Pascal Cuoq's excellent answer:

The Multiplication of Floating Point numbers basically consists of

  • Multiplying the mantissas
  • Adding the exponents
  • Normalizing the result

In-between, the result of multiplying the mantissas is rounded when it can not be represented exactly with the available 23bits (float) or 52 bits (double).

The behavior is described in the Java Language Specification:

The Java programming language requires that floating-point arithmetic behave as if every floating-point operator rounded its floating-point result to the result precision. Inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, the one with its least significant bit zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to nearest.

And as Pascal Cuoq already said: The value 4.35f can not be presented exactly. When this value is multiplied with 100.0f, the nearest representable value for the result in the above mentioned sense is 435.0. In contrast to that, for the multiplication of 4.35d and 100.0d, the nearest representable value for the result is 434.99999999999994.... And in both cases, the values are truncated to int by simply omitting the fractional part, yielding the values of 435 for the float case, and 434 for the double case, respectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.