Jump to content

In various programming languages, particularly C++ and Java, integral data types are pretty straight forward. They have 32/64 (int & long)bits of memory, and for every +1 or -1 in the value will update the binary code, until it reaches the limit, but the decimal data types are different. Float and Double have the same amount of memory allocated as Int and Long respectively, but decimal data types can go beyond -2^31 - 2^31-1 and 2^63 - 2^63-1, and that is only in the whole number part. The same thing applies in decimal section as well, and the weirdest part is that they keep losing precision the more you keep increasing the value, until the compiler throws an error. Losing precision aka making mistake by a computer???

PLEASE MARK COMMENTS AS SOLUTION IF SATISFIED!!

bigger number better, makes me look cooler.

Link to comment
https://linustechtips.com/topic/1451999-how-do-floats-or-doubles-work/
Share on other sites

Link to post
Share on other sites

42 minutes ago, RTXboy123 said:

Losing precision aka making mistake by a computer???

You may want to read about IEEE 754 encoding. Essentially those 32/64 bit are used to encode a number. Since there's a finite number of bits and an infinite number of possible values, yes you do lose some precision. https://en.wikipedia.org/wiki/Double-precision_floating-point_format

 

This is not "a mistake" in the sense that the computer is doing something wrong. The result is predictable and deterministic. The computer is still doing what it's designed to do.

 

When you e.g. multiply floating point numbers that are small enough you can get back a result that is incorrect (or rather imprecise). In most cases that error is small enough so it doesn't matter (at least in games). Which is why other data types (such as BigDecimal) exist for e.g. finance and other fields where it does.

 

For example when you're rotating a 3D object, you always want to take the original vertices, then apply the current absolute rotational angles, instead of taking the previous result and applying the relative rotational angles. Otherwise your will note that your object will slowly deform over time as these computational errors accumulate.

Remember to either quote or @mention others, so they are notified of your reply

Link to post
Share on other sites

The brief explanation for why float/doubles have error in basically certain decimals don't behave nice with base 2. Take the fraction 1/3, if you do the math with that you'll end up with the number 0.333333333333333, but that isn't actually what that number is equal to because there would be 3s going on until infinity. The same thing happens with certain float values, except instead of it being in base 10 (traditional counting numbers), it's in base 2. 

 

Floats/doubles behave a bit like scientific notation (not exactly, there are other parts for them that I'm going to omit because floats are super complex and I barely understand how they work, but for the purposes of this explanation it's good enough), they have a portion dedicated to the actual number itself, and then a portion dedicated to where the decimal place has to go. Since the whole number has to fit into a fixed size, as the decimal place gets closer and closer to the extremes the space dedicated to the decimal location has to get larger as well, and thus the space for the number itself gets smaller and smaller, thus losing more precision. 

 

For more detailed explanations (I only really have the knowledge you need for a hobbyist programmer for how they work), there are tons of resources from the actual documentation behind the floating point standard (IIRC that is free to access and view if you want to see technical reasoning behind each and every bit in a floating point and how to read them) to videos from places like Computerphile explaining how they work. You're probably better off watching one of those videos anyway than trying to read my probably slightly off explanation anyway.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×