Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 7 of 7
Thread: Mathematical error

01272013, 03:55 PM #1
 Join Date
 Jan 2013
 Posts
 9
 Thanks
 2
 Thanked 0 Times in 0 Posts
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.

01272013, 04:16 PM #2
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.

01272013, 04:38 PM #3
 Join Date
 Jan 2013
 Posts
 9
 Thanks
 2
 Thanked 0 Times in 0 Posts
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)

01272013, 04:49 PM #4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 19,342
 Thanks
 217
 Thanked 2,681 Times in 2,657 Posts
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; 01272013 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 (01282013)

01272013, 05:14 PM #5
 Join Date
 Jan 2013
 Location
 Germany
 Posts
 578
 Thanks
 4
 Thanked 77 Times in 77 Posts
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.

01272013, 09:45 PM #6
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 9,249
 Thanks
 4
 Thanked 932 Times in 919 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 (01282013)

01282013, 01:37 AM #7
 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.