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   
Tim Slattery
 
Posts: n/a

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






"McKirahan" <News (AT) McKirahan (DOT) com> wrote:

Quote:
How do I add floating point numbers accurately?

The following adds the 4 numbers
46.57, 45.00, 45.00, and 54.83 to give
191.39999999999998 instead of 191.40.
That's the way it is. Not all decimal fractions can be represented
exactly as binary fractions, which is how floating point numbers are
stored. Floating point numbers are necessarily somewhat imprecise. In
this case the imprecision is on the order of 10**-12, which is
extremely tiny.

--
Tim Slattery
Slattery_T (AT) bls (DOT) gov


Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Math Problem - 10-05-2005 , 07:29 PM






Tim Slattery wrote:
Quote:
McKirahan wrote:
How do I add floating point numbers accurately?

The following adds the 4 numbers
46.57, 45.00, 45.00, and 54.83 to give
191.39999999999998 instead of 191.40.

That's the way it is. Not all decimal fractions can be
represented exactly as binary fractions, which is how
floating point numbers are stored. Floating point numbers
are necessarily somewhat imprecise. In this case the
imprecision is on the order of 10**-12, which is extremely
tiny.
And it isn't a characteristic restricted to binary representations of
numbers. How precise is the decimal fraction representation of 1/3? And
would it be reasonable to complain that 3.33 + 3.33 + 3.33 did not
result in 1?

Richard.




Reply With Quote
  #3  
Old   
Randy Webb
 
Posts: n/a

Default Re: Math Problem - 10-05-2005 , 10:46 PM



Richard Cornford said the following on 10/5/2005 7:29 PM:

Quote:
Tim Slattery wrote:

McKirahan wrote:

How do I add floating point numbers accurately?

The following adds the 4 numbers
46.57, 45.00, 45.00, and 54.83 to give
191.39999999999998 instead of 191.40.

That's the way it is. Not all decimal fractions can be
represented exactly as binary fractions, which is how
floating point numbers are stored. Floating point numbers
are necessarily somewhat imprecise. In this case the
imprecision is on the order of 10**-12, which is extremely
tiny.


And it isn't a characteristic restricted to binary representations of
numbers. How precise is the decimal fraction representation of 1/3? And
would it be reasonable to complain that 3.33 + 3.33 + 3.33 did not
result in 1?
Umm, yes it would since it should either result in 9.99 or 10, not 1

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly


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.