Results 1 to 10 of 10
  1.    #1  
    Hey guys, I just ran into a weird issue with adding numbers in javascript. Most numbers add up fine, but when I try to add 23.2 and 33.9, I always get 57.099999999999994, instead of 57.1. W-T-F?

    Here are some different ways I found online for adding numbers, all with the same result.

    var add1 = parseFloat(23.2) + parseFloat(33.9);
    Mojo.Log.error("Added 1: " + add1) ;
    var add2 = 23.2 + 33.9;
    Mojo.Log.error("Added 2: " + add2) ;
    var add3 = (23.2 * 1) + (33.9 * 1);
    Mojo.Log.error("Added 3: " + add3) ;
    var add4 = +23.2 + +33.9;
    Mojo.Log.error("Added 4: " + add4) ;
    var add5 = eval(23.2) + eval(33.9);
    Mojo.Log.error("Added 5: " + add5) ;

    Try it, very weird.

    Thanks!

    David B.
  2. #2  
    Not far out. Javascript does everything using floating point.
  3. ardiri's Avatar
    Posts
    147 Posts
    Global Posts
    218 Global Posts
    #3  
    you may want to look at the round() function and apply it

    Number rounding in JavaScript
    // Aaron Ardiri
    Mobile 1UP, http://www.mobile1up.com/
    Caveman, Caveman HD (touchpad), Cronk and GW Series available on web OS
  4. #4  
    I think the Math.round() function is sort-of irrelevant here. The real topic is that it seems to be fundamental flaw in JavaScript as a whole. I get the same results across all major web-browsers.
    danNielson.com - Slalom - twitter - facebook
    A posted thanks dies with the thread. Clicking the button lasts until the admins get bored with this site and throw it in the recycling bin.
  5. #5  
    Quote Originally Posted by unfairSurprisery View Post
    I think the Math.round() function is sort-of irrelevant here. The real topic is that it seems to be fundamental flaw in JavaScript as a whole. I get the same results across all major web-browsers.
    Math.round() has everything to do with it. Floating point cannot precisely represent the result. It is up to the programmer to work within the accuracy limitations and display the result with the appropriate precision. In the original example the error is 1 in many thousands of billions.

    Given that the original example used data with one decimal point of precision. The appropriate action is to apply result=Math.round(original*10)/10 to the result.
  6. ardiri's Avatar
    Posts
    147 Posts
    Global Posts
    218 Global Posts
    #6  
    Quote Originally Posted by unfairSurprisery View Post
    I think the Math.round() function is sort-of irrelevant here. The real topic is that it seems to be fundamental flaw in JavaScript as a whole. I get the same results across all major web-browsers.
    i tell my developers that there are never any problems; only solutions. maybe you should start thinking the same. if it is a flaw, find a fix and use it. quit *****ing.
    // Aaron Ardiri
    Mobile 1UP, http://www.mobile1up.com/
    Caveman, Caveman HD (touchpad), Cronk and GW Series available on web OS
  7. #7  
    Just like 1/3 is a repeating decimal in base-10, sometimes numbers have a repeating decimal in binary. When that happens, you get results like yours. The only way to avoid this is to use integer arithmetic or to take it into account in your code and use round() to intelligently adjust your result.
  8. #8  
    i tell my developers that there are never any problems; only solutions. maybe you should start thinking the same. if it is a flaw, find a fix and use it. quit *****ing.
    Not *****ing. just saying that the OP wasn't a question 'how do I fix this?' It was 'hey I found this odd glitch'
    danNielson.com - Slalom - twitter - facebook
    A posted thanks dies with the thread. Clicking the button lasts until the admins get bored with this site and throw it in the recycling bin.
  9. ardiri's Avatar
    Posts
    147 Posts
    Global Posts
    218 Global Posts
    #9  
    Quote Originally Posted by unfairSurprisery View Post
    Not *****ing. just saying that the OP wasn't a question 'how do I fix this?' It was 'hey I found this odd glitch'
    i must admit; i had a bad day - sorry

    this is a well known issue - even if you write code in C, Pascal, Java, C# - you will run into the same issue. the IEEE standard for floating point is what causes this problem. the binary format overflows when it comes to decimals - which is why you cannot even reliably check if a floating point number is "equal", you can only check if it is close.
    // Aaron Ardiri
    Mobile 1UP, http://www.mobile1up.com/
    Caveman, Caveman HD (touchpad), Cronk and GW Series available on web OS
  10. #10  
    Quote Originally Posted by ardiri View Post
    i must admit; i had a bad day - sorry

    this is a well known issue - even if you write code in C, Pascal, Java, C# - you will run into the same issue. the IEEE standard for floating point is what causes this problem. the binary format overflows when it comes to decimals - which is why you cannot even reliably check if a floating point number is "equal", you can only check if it is close.
    NP. And thanks for that info about it being IEEE. Just checked, and Python does it too. Kind of an odd situation.
    danNielson.com - Slalom - twitter - facebook
    A posted thanks dies with the thread. Clicking the button lasts until the admins get bored with this site and throw it in the recycling bin.

Posting Permissions