Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Mathematical error

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-27-2013, 04: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 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.
 01-27-2013, 05:16 PM PM User | #2 jmrker Senior Coder     Join Date: Aug 2006 Location: FL Posts: 2,935 Thanks: 36 Thanked 477 Times in 471 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.
01-27-2013, 05: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
Quote:
 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)

01-27-2013, 05:49 PM   PM User | #4
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,481
Thanks: 200
Thanked 2,470 Times in 2,448 Posts
Quote:
 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.
__________________

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 06:10 PM..

 Users who have thanked Philip M for this post: tomaz42 (01-28-2013)
01-27-2013, 06:14 PM   PM User | #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 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.

 01-27-2013, 10:45 PM PM User | #6 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,094 Thanks: 0 Thanked 579 Times in 569 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/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.
 Users who have thanked felgall for this post: tomaz42 (01-28-2013)
 01-28-2013, 02: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 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.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 03:59 PM.