Skip to main content
Participating Frequently
November 3, 2010
Answered

Bug in multiplication in AS3 (CS5) ?

  • November 3, 2010
  • 1 reply
  • 1313 views

Hi,

When I try to multiply certain numbers, I get some weird results.

I cannot imagine that a simple multiplication would contain bugs, but it appears to be so...

Could someone please verify the following ?

Make a new AS3 flash project, and in the first frame, just paste the following code :

        /////////////////////////////////////////////////////////////////////

import flash.text.TextField

var a:Number = 19.8;
var b:Number = 9;
var result:Number = a * b;

var v_TextField = new TextField();
v_TextField.autoSize = TextFieldAutoSize.LEFT;
v_TextField.wordWrap = false;
v_TextField.text = "Test : " + a + "*" + b + "=" + result;

addChild(v_TextField);

/////////////////////////////////////////////////////////////////////

If I publish and run this, I get :

         Test : 19.8*9=178.20000000000002


The same behaviour with 19.8*18

I didn't look for other combinations yet.

What's up with this ?

This topic has been closed for replies.
Correct answer kglad

that's the correct answer within the accuracy used by flash ~ 16 digits.  the inaccuracy is introduced because computers use binary (and not decimal) arithmetic.  you can use rounding to get the answers you expect.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 3, 2010

that's the correct answer within the accuracy used by flash ~ 16 digits.  the inaccuracy is introduced because computers use binary (and not decimal) arithmetic.  you can use rounding to get the answers you expect.

Participating Frequently
November 3, 2010

Stupid I didn't think of that,

thanks for clearing it out !

Z.

kglad
Community Expert
Community Expert
November 3, 2010

you're welcome.