HighDots Forums  

Re: Math Problem

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: Math Problem in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Duncan Booth
 
Posts: n/a

Default Re: Math Problem - 10-05-2005 , 08:08 AM






McKirahan wrote:

Quote:
How do I add floating point numbers accurately?
Using '+'.

A simpler demonstration of your problem:
alert(136.57+54.83);
shows 191.39999999999998


Quote:
P.S. This FAQ doesn't help:
http://jibbering.com/faq/#FAQ4
4.7 Why does 5 * 1.015 != 5.075 or 0.05+0.01 != 0.06?


It should help. It says:

Quote:
Javascript numbers are represented in binary as IEEE-754 Doubles, with
a resolution of 53 bits, giving an accuracy of 15-16 decimal digits;
integers up to about 9e15 are precise, but few decimal fractions are.
in this case 136.57 is stored internally as 136.56999999999999,
54.83 is stored internally as 54.829999999999998.

Adding these together and reducing the result to the appropriate precision
gives the answer displayed above.

If you want to display a friendlier value as the result, change your final
alert to:
alert(t.toFixed(2) + " = 191.40 ?");


Reply With Quote
Reply




Thread Tools
Display Modes

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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.