Status

()

Core
JavaScript Engine
P3
normal
RESOLVED DUPLICATE of bug 5856
18 years ago
9 years ago

People

(Reporter: Sitsofe Wheeler, Assigned: Mike McCabe)

Tracking

Trunk
x86
Windows NT
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

(URL)

(Reporter)

Description

18 years ago
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

18 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
Last Resolved: 18 years ago
Resolution: --- → INVALID

Comment 2

18 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

17 years ago
Mozilla compilies with Standards so its all good :)
Status: RESOLVED → VERIFIED

Comment 4

15 years ago
*** Bug 202827 has been marked as a duplicate of this bug. ***

Comment 5

15 years ago
*** Bug 210874 has been marked as a duplicate of this bug. ***

Comment 6

14 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

13 years ago
*** Bug 280890 has been marked as a duplicate of this bug. ***
*** Bug 280890 has been marked as a duplicate of this bug. ***

Updated

13 years ago
QA Contact: rginda → nobody

Updated

13 years ago
Status: VERIFIED → REOPENED
Resolution: INVALID → ---

Comment 10

13 years ago

*** This bug has been marked as a duplicate of 5856 ***
Status: REOPENED → RESOLVED
Last Resolved: 18 years ago13 years ago
Resolution: --- → DUPLICATE
Duplicate of this bug: 368288

Updated

10 years ago
Duplicate of this bug: 408202
Duplicate of this bug: 442766
You need to log in before you can comment on or make changes to this bug.