Frage

Well i just got a problem, with the simple following code:

trace( 0.01+0.05 );  // 0.060000000000000005
trace( 0.03-0.01 );  // 0.019999999999999997

I mean i just want 0.01+0.05 give me 0.06 and 0.03-0.01 give me 0.02. Does someone have an idea how to retrieve the correct results ?

War es hilfreich?

Lösung

The imprecision is due to floating point arithmetic. 0.01, 0.05 and 0.03 are all floating point literals. Not every number (in fact, very few numbers) can be represented precisely in floating point.

For example, 0.5 can be but, 0.06 cannot. As a rule of thumb the first 15 significant figures will be correct.

For more details, see http://en.wikipedia.org/wiki/Floating_point

Andere Tipps

trace(Math.format((0.01+0.05), 2));

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top