ID:71751
 
Not a bug
Not a bug
BYOND Version:441
Operating System:Windows Vista Business 64-bit
Web Browser:Opera 9.51
Status: Not a bug

This is not a bug. It may be an incorrect use of syntax or a limitation in the software. For further discussion on the matter, please consult the BYOND forums.
Duplicates:id:65769
Descriptive Problem Summary:
For some reason, BYOND is bad at involving math that includes subtracting numbers smaller than 1.
An example is having a variable at 1.2, then subtracting 0.4 from it 3 times, it should go from 1.2, to 0.8, to 0.4, to 0. But when the number should reach 0, it does not. Instead it turns into some crazy number like 5.96046e-008.

It may also work with other numbers, but I have only tried subtracting from a number that is a multiple of 0.4 (0.4, 0.8, 1.2, 1.6, 2 and so on).

Numbered Steps to Reproduce Problem:
Just follow the code snipper below to see what I mean

Code Snippet (if applicable) to Reproduce Problem:
mob/verb/CrazyMath()
var/a=1.2
usr << a //1.2
a-=0.4
usr << a //0.8
a-=0.4
usr << a //0.4
a-=0.4
usr << a //0


Expected Results: a should be 0.

Actual Results:It is actually 5.96046e-008 (when I test it).

Does the problem occur:
Every time? Or how often? Everytime
In other games? Yep
In other user accounts? Most likely
On other computers? No idea, cannot test

When does the problem NOT occur?

Workarounds:
You can round the number down.
Looks like it uses scientific notation for decimal numbers and has trouble switching back to whole numbers... Doesn't make a stitch of sense to me really :D
This is not a bug. BYOND uses 32-bit IEEE floating point numbers, which have 24 bits of precision. Decimal numbers like 0.4 are repeating numbers in binary representation, thus they are not in exact form. Specifically, 0.4 is represented as:

0.01100110011...

In floating point notation this is 1.10011001... multiplied by 2^(-2).

This is a known caveat of computer math on any platform. The only way to get around this is to use a special math library that tries its best to work with exact representations of numbers, or (in this case) use a BCD number system. If you want to always work with multiples of 0.1, though, you can always round the result of any operation to the nearest 0.1 and that should undo most of the subtle rounding errors that can crop up.