Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Mathematical error

    I've got this code below... which adds up the values of an array (into chancesGone), and attempts to work out the percentage of each array value. The problem is that the numbers are coming back slightly off, and I'm not sure why. Even when there is only 1 number in the array, the result is slightly over 100.

    for(var i=1; i< Pick; i++){
    chancesGone += parseInt(document.getElementById("chanceSolid["+pickOrder[i]+"]").innerHTML);
    }

    for(var a=1; a< Pick; a++){
    document.getElementById("chanceSolid["+pickOrder[a]+"]").innerHTML = ((document.getElementById("chanceSolid["+pickOrder[a]+"]").innerHTML)/chancesGone*100).toFixed(2);
    }

    Thanks for your help.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,092
    Thanks
    38
    Thanked 498 Times in 492 Posts
    The .toFixed function returns a string value rounded. The rounding process may lead to small errors in the total sum over 100 array element values.

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jmrker View Post
    The .toFixed function returns a string value rounded. The rounding process may lead to small errors in the total sum over 100 array element values.
    But it still doesn't make sense for the situation where there is only 1 number in the array.

    Eg. if the number is 30.25
    chancesGone = 30.25
    result would be = 30.25/30.25 * 100 (which is surely only rounded once the calculation is done, leaving 100.00... but it is still returning slightly over 100)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,082
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by tomaz42 View Post
    But it still doesn't make sense for the situation where there is only 1 number in the array.

    Eg. if the number is 30.25
    chancesGone = 30.25
    result would be = 30.25/30.25 * 100 (which is surely only rounded once the calculation is done, leaving 100.00... but it is still returning slightly over 100)
    Huh? Why do you say that?

    var x = 30.25/30.25 *100;
    alert (x); // 100

    chancesGone += parseInt(document.getElementById("chanceSolid["+pickOrder[i]+"]").innerHTML);
    Change parseInt() to parseFloat() or Number();

    In some cases small errors arise because computers work in binary so they convert your number to the nearest binary equivalent first and then convert the binary back into the nearest decimal equivalent at the end.

    A few examples:-

    alert (4.935*100);
    alert (3355.53 + 660.97 - 660.97);
    alert (1.1 * 1.1);
    alert (5.5%2.2);

    The greatest miracle in the bible is when Joshua told his son to stand still and he obeyed him.
    - Pupil's answer to Catholic Elementary School test.
    Last edited by Philip M; 01-27-2013 at 05:10 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    tomaz42 (01-28-2013)

  • #5
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    Quote Originally Posted by Philip M View Post
    In some cases small errors arise because computers work in binary so they convert your number to the nearest binary equivalent]
    This is completely correct, but I feel like it's worth stressing this even more: A computer cannot store every number with exact precision – it simply can't.

    And that makes a lot of sense. If you store a floating point number with single precision, you have 32 bit of information. In particular, this number is finite and since it only uses a finite set of symbols (in fact only two: 0 and 1), the amount of numbers that can be stored is finite. However, even the set of integers is already infinite, let alone the set of real numbers which is "even more infinite" than infinite (we call it uncountable).

    This is an important piece of knowledge for any programmer.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    If you want exact precision with calculations on a computer then you need touse integers and not fractions. Multiply all the numbers by a multiple of ten big enough so that the calculation will not involve fractions before you do the calculations and divide the result by the appropriate multiple of ten at the end, This will then maintain exact values provided that you don't exceed the maximum precision the computer uses for numbers.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    tomaz42 (01-28-2013)

  • #7
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you all for your help!

    It looks to be working well now

    The mixture of using parseFloat and further multiplying the number stored seems to be what did the trick.


  •  

    Posting Permissions

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