How is this not correct? (Page 1) ● SmileBASIC Source Forums

### Sign In

Register
*Usernames are case-sensitive
Forgot my password

# How is this not correct?

• #1 ✎ 65 Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website `? 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. Posted Edited by Gaelstrom_Valence
• #2 ✎ 1690 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself 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) Posted
• #3 ✎ 91 Simeon Scholar Received for knowing a great deal about programming topics Achievements Amazing Page Hidden Achievements Drawing I like to draw! Hobbies 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 Posted Edited by Simeon
• #4 ✎ 1690 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself ```DEF ALMOSTEQUAL(A,B) RETURN ABS(A-B)<0.0000001 END``` Posted
• #5 ✎ 65 Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website Eyyyyyy Posted