# Bad result on a rest between 2 floats

 0 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? more ▼ asked Jan 04 '12 at 10:58 PM DarkSlash 41 ● 12 ● 14 ● 16 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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. more ▼ answered Jan 04 '12 at 11:53 PM aldonaletto 42.5k ● 16 ● 43 ● 202 Flawless answer! That was the problem! Thanks! :) Jan 05 '12 at 06:01 PM DarkSlash add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x224
x185
x9

asked: Jan 04 '12 at 10:58 PM

Seen: 449 times

Last Updated: Jan 05 '12 at 07:15 PM