Discord
Login
Community
DARK THEME

Why does my code go to decimals, when the math says it won't?

So, I was making an upgrade for a clicking game im making, and I just want to know why this turns to decimals, here all the code:

money = 0 tupgrade = 0 tprice = 100 aupgrade = 0 aprice = 0 click = 1

if keyboard.press.1 and money >= tprice then money = money-tprice tprice = tprice*1.2 tupgrade = tupgrade+1 click = click+1 end

Just in case, I also made an unlisted version of it. https://microstudio.dev/i/JustNitro/clickersimtest/

( in advance, I read this and it sounds really condescending, I dont mean for it to be, sorry :( )

Ok, so, the price of an upgrade is 144. Now you are multiplying that by 1.2 (not the original 100, the new 144). 144 * 1.2 = 172.8. The game giving you 172.7999999999999999998 is weird, but I would presume it has something to do with floating point precision.

THE FIX: Personally, I would just change tprice = tprice1.2 into tprice = round(tprice1.2) to round to the nearest integer.

This is due to the limits of the 64 bits floating point numbers standard. For more information, have a look here: https://microstudio.dev/community/bugs/minor-innacuracies-with-math-functions/141/3/

The workaround is to use rounding, whenever necessary, as @JmeJuniper suggests.

Thanks, I will keep these in mind next time.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community