LoginLogin

How is this not correct?

Root / Programming Questions / [.]

Gaelstrom_ValenceCreated:
? 129.98-129==0.98
I don't know if it's just me, but this always comes out as false, along with any combination of subtracting a value by it's Floored value. EDIT: It seems subtraction with decimals in general is a little wonky.

Math with floating point numbers is usually not perfect. The same problem happens in almost every other language too. What you can do is check if ABS(129.98-129 - 0.98) is less than a really small number like 0.000001, or use whole numbers instead (12998 - 12900 == 98)

If you try
PRINT (129.98-129)*1000000
you will get "979999.99999999" But, strangely, when you switch things around,
(129.98-0.98)==129
it returns true. My advice is if precision is important, then keep everything as a floating point number, or everything as an integer. But don't go between the two. The best explanation I can give is, it's returning a float, but you want it to be an int. Even though it's a precision bug, it seems to easy to work around. This sort of thing happens in lots of programming languages It's still really strange that the only way to see the tiny difference is by blowing up the number. This works though
PRINT STR$(129.98-129)=="0.98"
returning true

DEF ALMOSTEQUAL(A,B)
 RETURN ABS(A-B)<0.0000001
END

Eyyyyyy