A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: Why can't Flash 8 do math correctly?

  1. #1
    Junior Member
    Join Date
    Apr 2006
    Posts
    7

    Why can't Flash 8 do math correctly?

    //thanks deadbeat.

    Hi, I am running the following script. The problems is that the answer I get it something like 0.0499999999999998 instead of 0.05 and I was wondering why.

    function checkChange(cost, moneyTendered, moneyReturned){
    var correctChange:Number = 0;
    var difference:Number = 0;
    trace ("I am checking my change...");
    correctChange = moneyTendered - cost;
    difference = correctChange - moneyReturned;
    if (correctChange == moneyReturned) {
    trace("\tCorrect change given");
    } else {
    trace("\tIncorrect amount! by $" + difference);
    }
    }

    checkChange(12.5,15,2.55);
    Last edited by ushiroda80; 07-08-2006 at 11:45 AM.

  2. #2
    FK's Official Mac Hater jasonsplace's Avatar
    Join Date
    Mar 2002
    Location
    Provo, Utah
    Posts
    2,245
    It looks like you may have found a bug unless someone else can come up with a reason that 2.5-2.55 = 4.999999998
    Jason L. Wright
    I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.

  3. #3
    The CROW!!!
    Join Date
    Mar 2003
    Location
    somewhere. i think. not sure.
    Posts
    90
    I would suggest using Math.round().
    I Haunt the Shadows...I stalk the blackness of night... I am...
    THE CROW!!!

  4. #4
    FK's Official Mac Hater jasonsplace's Avatar
    Join Date
    Mar 2002
    Location
    Provo, Utah
    Posts
    2,245
    Quote Originally Posted by The_lone1
    I would suggest using Math.round().
    That was my first thought as well but it doesn't leave it as a decimal and in this case just rounds to 0....which i'm guessing won't work in this situation.
    Jason L. Wright
    I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.

  5. #5
    The CROW!!!
    Join Date
    Mar 2003
    Location
    somewhere. i think. not sure.
    Posts
    90
    I reckon this is probably the same limitation every single computer will encounter. It's based on the fact that all data is stored in binary, and the smaller the number is, the less likely you'll be able to store it exactly.
    I Haunt the Shadows...I stalk the blackness of night... I am...
    THE CROW!!!

  6. #6
    Dignitary rynoe's Avatar
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    760
    It gets even more strange:
    [CODE]
    var a:Number = 0.5 - 0.55;
    var b:Number = 1.5 - 1.55;
    var c:Number = 2.5 - 2.55;
    var d:Number = 3.5 - 3.55;
    var e:Number = 4.5 - 4.55;
    trace( "zero: " + a);
    trace( "one: " + b);
    trace( "two: " + c);
    trace( "three: " + d);
    trace( "four: " + e);
    [CODE]


    Output:
    zero: -0.05
    one: -0.05
    two: -0.0499999999999998
    three: -0.0499999999999998
    [SIGPIC][/SIGPIC]

  7. #7
    The CROW!!!
    Join Date
    Mar 2003
    Location
    somewhere. i think. not sure.
    Posts
    90
    hmm. looks more like the smaller proportion the remainder is of the original number, the more likely it is to get a math error...
    I Haunt the Shadows...I stalk the blackness of night... I am...
    THE CROW!!!

  8. #8
    Senior Member
    Join Date
    Nov 2005
    Location
    dante's inferno
    Posts
    904
    Math.round won't work, .5 would round up to 1.
    this is strange, I have mx2004pro, and it did the same thing.
    if you put trace(2.5-2 );it will give you .5

    I got this to work once, all the other tries the answer just kept getting worse

    IMS

  9. #9
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Sounds like you've run into the rounding error:

    http://www.adobe.com/cfusion/knowled...fm?id=tn_13989

    K.

  10. #10
    Senior Member
    Join Date
    Nov 2005
    Location
    dante's inferno
    Posts
    904
    If you give back too much change it seems to work fine, but if it is correct or less, I've been getting errors.... I'm bored outta my cap right now, so this is helping the day go a little quicker

    IMS

  11. #11
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    meh, nevermind
    Last edited by dudeqwerty; 07-06-2006 at 07:04 PM.
    New sig soon

  12. #12
    The CROW!!!
    Join Date
    Mar 2003
    Location
    somewhere. i think. not sure.
    Posts
    90
    Quote Originally Posted by dudeqwerty
    Mate, what are you talking about?
    it's impossible to represent some numbers in binary. most of these are small negative decimals., as evidenced in this case.
    I Haunt the Shadows...I stalk the blackness of night... I am...
    THE CROW!!!

  13. #13
    Senior Member dudeqwerty's Avatar
    Join Date
    Mar 2005
    Location
    Bosnia
    Posts
    1,626
    yeah i got you, i though you were saying something else, hence my edit
    New sig soon

  14. #14
    The CROW!!!
    Join Date
    Mar 2003
    Location
    somewhere. i think. not sure.
    Posts
    90
    oh right.
    I Haunt the Shadows...I stalk the blackness of night... I am...
    THE CROW!!!

  15. #15
    Member
    Join Date
    Apr 2004
    Posts
    49
    zero: -0.050000000000000044
    one: -0.050000000000000044
    two: -0.04999999999999982
    three: -0.04999999999999982
    four: -0.04999999999999982

    results in flash 9 ... even weirder

    var a:Number = 0.5 - 0.55;
    var b:Number = 1.5 - 1.55;
    var c:Number = 2.5 - 2.55;
    var d:Number = 3.5 - 3.55;
    var e:Number = 4.5 - 4.55;
    trace( "zero: " + a);
    trace( "one: " + b);
    trace( "two: " + c);
    trace( "three: " + d);
    trace( "four: " + e);

  16. #16
    Junior Member
    Join Date
    Mar 2006
    Posts
    12
    after reading that roundoff error article on the adobe site, it does seem like thats the case...

    if Math.round only works on the units level, then just multiply your result by 100 first, then use Math.round(), then divide it by 100 again.

    does that make sense D:?

    kae

  17. #17
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    You probably solved this thread. I was wondering how one can do a simple computer calculation without loosing money
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center