Subtraction Problem in Java

Started by
6 comments, last by Pink Horror 9 years, 1 month ago

Why is System.out.println(1.4-1) producing "0.3999999999999999" as the difference in Java?

In Math, 1.4 - 1 is 0.4.

Advertisement

http://support.microsoft.com/kb/42980

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

That article is a bit wordy. Put more simply:

Treat floating point as an approximation. Changing the order of instructions can slightly change the results, but it should always be within the specified tolerance.

Variations within chip designs mean that even identical code with identical data can give slightly different floating point values on different processors. This is known and expected behavior.

The float type normally gives 6 decimal digits precision.

The double type normally gives 15 decimal digits precision.

Your answer is within the precision and accuracy requirements.

In math, 0.39999999999...(repeating to infinity) is equal to 0.4

In math, 0.39999999999...(repeating to infinity) is equal to 0.4

While this is true, the floating-point approximation shown in the OP doesn't repeat to infinity, e.g. if you kept asking for more decimals you'd eventually get something like 0.3999999761581420898437500... (float) or 0.3999999999999999111821580... (double) depending on what the particular result happens to be.

Still, close enough cool.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Okay so floating point arithmetic in Java is an approximation. Understood, thanks guys. Thanks for the link, Glass_Knife.

floating point arithmetic anything in Java anything is an approximation


FTFY. Even in math and science, 1.333e0 + 1.333e1 will result in some loss in precision (1.466e1 != 1.4663e0), for four significant digits. Floating point math is no different.

EDIT:
I can't math. sad.png


Okay so floating point arithmetic in Java is an approximation. Understood, thanks guys. Thanks for the link, Glass_Knife.

For a simplified explanation for why this happens, imagine how 2/5 might be written out in binary. It'll have to repeat forever. Your fraction's denominator has to be some power of 2 for it to end. In decimal, the denominator has to factor into 2s and 5s, so you can write 2/5 as just 0.4 instead of repeating as if you wanted the number to be 2/7 instead.

A floating point number looks something like this (I don't know if I've seen the implied 1 of the mantissa part written exactly this way but it seems equivalent to me):

(sign) x (1 + (mantissa) / (2 ^ (bits available for mantissa))) x 2 ^ ((exponent) - (exponent bias))

The mantissa and exponent are non-negative integers. The size of the mantissa, the bias, and the range for the exponent are defined by the floating point specification. Any fraction that comes out of that is only going to have a power of 2 on the bottom. There isn't a way to get anything else down there. The format is like scientific notation written in binary instead of decimal.

This topic is closed to new replies.

Advertisement