? 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 ✎ 67 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 ✎ 1619 12Me21 Head Admin Second Year My account is over 2 years old Website 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 tryPRINT (129.98-129)*1000000you will get "979999.99999999" But, strangely, when you switch things around,(129.98-0.98)==129it 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 thoughPRINT STR$(129.98-129)=="0.98" returning true Posted Edited by Simeon
  • #4 ✎ 1619 12Me21 Head Admin Second Year My account is over 2 years old Website 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 ✎ 67 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