Inaccurate floating/double numbers

BlitzMax Forums/BlitzMax Programming/Inaccurate floating/double numbers

It has been bothering me for a while.
When I use the data types float or double to a variable and assign a number 0.02 and later retrieve it, it'll show up as something like 0.01932932 or so.
Is there a reason for that?

Its been mentioned about 14 million times before. Floating point innacuracy - nothing you can do about it (other than store the value as a string).

Here's some more information as per the Wikipedia article on Floating point

By their nature, all numbers expressed in floating-point format are rational numbers with a terminating expansion in the relevant base (for example, a terminating decimal expansion in base-10, or a terminating binary expansion in base-2). Irrational numbers, such as π or √2, or non-terminating rational numbers, must be approximated. The number of digits (or bits) of precision also limits the set of rational numbers that can be represented exactly. For example, the number 123456789 clearly cannot be exactly represented if only eight decimal digits of precision are available.
When a number is represented in some format (such as a character string) which is not a native floating-point representation supported in a computer implementation, then it will require a conversion before it can be used in that implementation. If the number can be represented exactly in the floating-point format then the conversion is exact. If there is not an exact representation then the conversion requires a choice of which floating-point number to use to represent the original value. The representation chosen will have a different value to the original, and the value thus adjusted is called the rounded value.
Whether or not a rational number has a terminating expansion depends on the base. For example, in base-10 the number 1/2 has a terminating expansion (0.5) while the number 1/3 does not (0.333...). In base-2 only rationals with denominators that are powers of 2 (such as 1/2 or 3/16) are terminating. Any rational with a denominator that has a prime factor other than 2 will have an infinite binary expansion. This means that numbers which appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point.



Of course, you can also store your values as rational numbers (fractions), and work your arithmetic on those... but if you need at some point, to show it on screen with a decimal place, you'll probably have to round it.

And using rationals is a massive faff, so not worth bothering with unless you're doing Science!

Nothing wrong with science! :-p

You might also try using fixed point, which is particularly nice if your numbers are not going to get too large. If you are within the range 0..65535, then you can just use `Shl 16` and `Shr 16` to convert between fixed point and integer. You will get better accuracy, in a way, than floating point, because you always get the same number of decimal places no matter where the number is in the range - whereas with floating point, smaller numbers get more decimal points than larger ones.

With that in mind, what if you store a float as 0.00002, does it come back correctly?