Bad math calculations
Charlysole
Tortosa, SpainMember, PRO Posts: 3
Hi, i am having some problems with basic math calculations...
Not me, a game with these actors:
There are 7 actors in total
3 for add and 3 for subtract (in real numbers)
1 for display the result
(On touch is pressed)
Actor A adds 10
Actor B adds 1
Actor C adds 0.01
Actor D subtract 10
Actor E subtract 1
Actor F subtract 0.01
F+E+D = 11.01 thats fine
but
11.01 - B - A = 0.01? (suposed to be)
but
NOT FOR ME
Result is 0.0099999999999998
Any explanation?
Thanks!
Comments
@Charlysole, floating point arithmetic errors is common in all computer languages. It is an artifact of its bit representation in memory.
If you need exact numbers, you should always round up to the significant number of decimal places.
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com
Thank you @Hopscotch now i understand why it behaves that way.
Can you teach me how to round up in GS?
This community is awsome, thank you so much
@Charlysole,
round(x) rounds to an integer
roundTo(x,n) rounds to n decimal places
prec(x,n) rounds to n decimal places and pads with zeros (ideal for displaying numbers)
ceil(x) next biggest integer
floor(x) integer part only
MESSAGING, X-PLATFORM LEADERBOARDS, OFFLINE-TIMER, ANALYTICS and BACK-END CONTROL for your GameSalad projects
www.APPFORMATIVE.com