Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## 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);
}

• 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.

• Originally Posted by jmrker
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)

• Originally Posted by tomaz42
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;

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 (3355.53 + 660.97 - 660.97);

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.

• ## Users who have thanked Philip M for this post:

tomaz42 (01-28-2013)

• Originally Posted by Philip M
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.

• 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.

• ## Users who have thanked felgall for this post:

tomaz42 (01-28-2013)

• 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
•