質問

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 ?

役に立ちましたか?

解決

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top