Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Subtraction Problem in Java

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

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

Share this post


Link to post
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 this post


Link to post
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. sad.png
Edited by fastcall22

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!