Closed Bug 202827 Opened 22 years ago Closed 22 years ago

Adding some floating point numbers incorrectly using the + operator

Categories

(Core :: JavaScript Engine, defect)

x86
Windows XP
defect
Not set
major

Tracking

()

VERIFIED DUPLICATE of bug 20140

People

(Reporter: william.foreman, Assigned: rogerl)

Details

User-Agent:       Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
Build Identifier: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)

When I add 1.89 and 1 (1.89 + 1) using javascript, I get the answer 
2.8899999999999997. But if I add 1.89 and 10, I get 11.89.

This happens every time, but only with certain numbers. 

Reproducible: Always

Steps to Reproduce:
1. var num = 1.89 + 1;
2. alert num;
3.

Actual Results:  
I get a message box with the value 2.8899999999999997

Expected Results:  
Given me 2.89 :-)
This really does depend on your definition of correct :-)

Mozilla is following the appropriate standards here--the issue centers around
trying to represent base-10 numbers in a computer that does base-2 math.  See
the references in the duped bug for more info.

*** This bug has been marked as a duplicate of 20140 ***
Status: UNCONFIRMED → RESOLVED
Closed: 22 years ago
Resolution: --- → DUPLICATE
Marking Verified Duplicate. Note that JavaScript is not the only
language in which this behavior occurs. Also C, C++, Java, etc.

Here are some more references:

bug 42134  "JavaScript should be more lenient comparing (==) floats"
bug 129300 "Error in javascript internal routine : parseFloat"
bug 154103 "arithmetic error in JavaScript Engine"
bug 160173 "Error de punto flotante al sumar en JavaScript"
bug 186563 "Number.toFixed: loss of precision"
bug 190303 "Javascript floating point calculation"

Good web references: 
http://developer.netscape.com/docs/manuals/js/core/jsref15/number.html#1200964
http://www.merlyn.demon.co.uk/js-maths.htm
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.