Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 03:55 PM   PM User | #1
tomaz42
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
tomaz42 is an unknown quantity at this point
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.
tomaz42 is offline   Reply With Quote
Old 01-27-2013, 04:16 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Old 01-27-2013, 04:38 PM   PM User | #3
tomaz42
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
tomaz42 is an unknown quantity at this point
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)
tomaz42 is offline   Reply With Quote
Old 01-27-2013, 04:49 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,105
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 01-27-2013 at 05:10 PM..
Philip M is online now   Reply With Quote
Users who have thanked Philip M for this post:
tomaz42 (01-28-2013)
Old 01-27-2013, 05:14 PM   PM User | #5
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
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.
Airblader is offline   Reply With Quote
Old 01-27-2013, 09:45 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,532
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
tomaz42 (01-28-2013)
Old 01-28-2013, 01:37 AM   PM User | #7
tomaz42
New to the CF scene

 
Join Date: Jan 2013
Posts: 9
Thanks: 2
Thanked 0 Times in 0 Posts
tomaz42 is an unknown quantity at this point
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.
tomaz42 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:08 AM.


Advertisement
Log in to turn off these ads.