Closed
Bug 20140
Opened 25 years ago
Closed 19 years ago
Rounding Error?
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: sitsofe, Assigned: mike+mozilla)
References
()
Details
The following does not produce the expected answer: <script language="javascript1.2"><!-- document.write("14.28 x 9 should be <b>128.52</b>;<br><small>your browsers' answer = </small><b>" + (14.28*9)+"</b>"); // --></script> Expected: 128.52 Result: 128.51999999999998 However, both IE 4.01 and Netscape 4.5 on the PC produce the same error. I don't think that this makes it right though... System: Build ID: 1999111520 WinNT 4 SP 6
Assignee | ||
Comment 1•25 years ago
|
||
I hate to say this, but it's not an error! JavaScript (like most programming languages) computes values using floating point numbers in a binary representation. Unfortunately, this format can't exactly represent many numbers we can represent exactly using decimal notation. One such number is 14.28... So when the decimal numbers we work with in JavaScript are converted to binary floating point representation internally, approximations are sometimes introduced; these sometimes show up in the output, as you noticed. For better or worse, this behavior is specified by the ECMA-262 standard to which JavaScript conforms. Thanks for the report! Sorry I can't give a better answer; if this is a problem for you, I believe that there are rounding routines available for JavaScript that should give you reasonable result.
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → INVALID
Comment 2•25 years ago
|
||
This behavior is covered both by the ISO standard 16262 (ECMAScript) and the ANSI/IEEE standard 754. You will find the same behavior in most other programming languages, including Java and almost all implementations of C/C++. If our implementation of JavaScript did not produce 128.51999999999998, we would be violating international floating-point arithmetic standards. In JS1.5 you can use Number.toFixed to round the answer to the number of decimal places you desire.
Comment 3•24 years ago
|
||
Mozilla compilies with Standards so its all good :)
Status: RESOLVED → VERIFIED
Comment 4•21 years ago
|
||
*** Bug 202827 has been marked as a duplicate of this bug. ***
Comment 5•21 years ago
|
||
*** Bug 210874 has been marked as a duplicate of this bug. ***
Comment 6•21 years ago
|
||
*** Bug 221816 has been marked as a duplicate of this bug. ***
*** Bug 261947 has been marked as a duplicate of this bug. ***
Comment 8•20 years ago
|
||
*** Bug 280890 has been marked as a duplicate of this bug. ***
Comment 9•20 years ago
|
||
*** Bug 280890 has been marked as a duplicate of this bug. ***
Updated•20 years ago
|
QA Contact: rginda → nobody
Updated•19 years ago
|
Status: VERIFIED → REOPENED
Resolution: INVALID → ---
Comment 10•19 years ago
|
||
*** This bug has been marked as a duplicate of 5856 ***
Status: REOPENED → RESOLVED
Closed: 25 years ago → 19 years ago
Resolution: --- → DUPLICATE
You need to log in
before you can comment on or make changes to this bug.
Description
•