dimanche 31 mai 2015

Decimal numbers not adding correctly in AS3

In my program, I have a score multiplier variable of type 'Number' When I try to add 0.1 to it, I have a problem. Here is the code:

scoreMultiplier += 0.1;

scoreMultiplier is originally set to 1, and after the first run through, I correctly get a value of 1.1. However, the second time, the trace shows 1.2000000000000002, and the next time 1.3000000000000003.

There is no other code which modifies scoreMultiplier. This is a problem, first because it is shown on my game screen and goes off the screen, and second because if conditionals where scoreMultiplier==2 for example do not work due to the bizarre fault in the addition.

If anyone knows what's causing this, or at the very least how to truncate the value to 1 decimal place, that would be great.

Aucun commentaire:

Enregistrer un commentaire