- Home /
Bad result on a rest between 2 floats
I have 2 floats variables: lets say "money" and "bet". At some point, I do money = money - bet; or money -= bet;
bet is from 0.05 to 1 and money starts at 10000. Lets take 0.05 for bet. The first rest, money equals 9999.95. The second rest, is 9999.91. And then alternates resting 0.05 or 0.04.
Why is this happening? Any idea?
Answer by aldonaletto · Jan 04, 2012 at 11:53 PM
The float precision is about 7 digits, thus you should not have problems with 10000 and 0.05 - but with 100000 and 0.05 this error would appear, for sure.
If you must work with such wide range of values, declare the variables as double:
var money: double = 1000000;
var bet: double = 0.05;
Double precision is about 16 digits, thus the player can even break the bank without loosing precision.
Your answer
Follow this Question
Related Questions
Multiply by float (Double, Decimal) 0 Answers
How do i get the part of a float after the point? 7 Answers
How do I add something on to a float. 1 Answer
Unity Float to db 2 Answers