Subtraction Problem in Java

This topic is 1386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

In Math, 1.4 - 1 is 0.4.

Share on other sites

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

Share on other sites

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

Edited by warnexus

Share on other sites

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.
Edited by fastcall22

Share on other sites

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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633663
• Total Posts
3013233
×

Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!