Closed Bug 20140 Opened 25 years ago Closed 19 years ago

Rounding Error?

Categories

(Core :: JavaScript Engine, defect, P3)

x86
Windows NT
defect

Tracking

()

RESOLVED DUPLICATE of bug 5856

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
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
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.
Mozilla compilies with Standards so its all good :)
Status: RESOLVED → VERIFIED
*** Bug 202827 has been marked as a duplicate of this bug. ***
*** Bug 210874 has been marked as a duplicate of this bug. ***
*** Bug 221816 has been marked as a duplicate of this bug. ***
*** Bug 261947 has been marked as a duplicate of this bug. ***
*** Bug 280890 has been marked as a duplicate of this bug. ***
*** Bug 280890 has been marked as a duplicate of this bug. ***
QA Contact: rginda → nobody
Status: VERIFIED → REOPENED
Resolution: INVALID → ---

*** This bug has been marked as a duplicate of 5856 ***
Status: REOPENED → RESOLVED
Closed: 25 years ago19 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.