Closed Bug 737776 Opened 14 years ago Closed 2 years ago

incorrect decimal rounding of small integers

Categories

(Core :: JavaScript Engine, defect)

x86_64
Windows 7
defect

Tracking

()

RESOLVED INACTIVE

People

(Reporter: roger.andrews, Unassigned)

Details

User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20100101 Firefox/11.0 Build ID: 20120312181643 Steps to reproduce: Decimal rounding using toExponential, toPrecision, toFixed. Large integers and fractions behave according to the ES5 Standard, but small integers do not. This bug is present in FF11.0 and has existed since at least FF3.6. Actual results: The ES5 Standard says that string conversions are rounded with ties away from 0. In firefox: Large integers and fractions are rounded with ties away from 0; Small integers (<1e15) are rounded with ties to the even value (Banker's Rounding) (as for arithmetic operators). Test procedure: I typed the following string conversions on the Firebug command line in Firefox 11.0. small integer FF11.0 nearTiesAway nearTiesEven (2.5e10).toExponential(0) "2e+10" "3e+10" "2e+10" (3.5e10).toExponential(0) "4e+10" "4e+10" "4e+10" (-2.5e10).toExponential(0) "-2e+10" "-3e+10" "-2e+10" (-3.5e10).toExponential(0) "-4e+10" "-4e+10" "-4e+10" (1.1125e10).toExponential(3) "1.112e+10" "1.113e+10" "1.112e+10" large integer FF11.0 nearTiesAway nearTiesEven (2.5e15).toExponential(0) "3e+15" "3e+15" "2e+15" (3.5e15).toExponential(0) "4e+15" "4e+15" "4e+15" (-2.5e15).toExponential(0) "-3e+15" "-3e+15" "-2e+15" (-3.5e15).toExponential(0) "-4e+15" "-4e+15" "-4e+15" (1.1125e15).toExponential(3) "1.113e+15" "1.113e+15" "1.112e+15" fraction FF11.0 nearTiesAway nearTiesEven (1.25).toExponential(1) "1.3e+0" "1.3e+0" "1.2e+0" (1.75).toExponential(1) "1.8e+0" "1.8e+0" "1.8e+0" (-1.25).toExponential(1) "-1.3e+0" "-1.3e+0" "-1.2e+0" (-1.75).toExponential(1) "-1.8e+0" "-1.8e+0" "-1.8e+0" (1.5).toExponential(0) "2e+0" "2e+0" "2e+0" (2.5).toExponential(0) "3e+0" "3e+0" "2e+0" The same effect happens with toPrecision, and with toFixed for fractions. (Nb. It is only fair to consider fractions that are representable in binary, such as 0.5, 0.25, 0.75, ..., otherwise binary rounding is done by the literal conversion before the formatter has a go.) Note that large integers (>1e15) are close to the maximum integer of ~9e15. It is strange to vary the rounding mode based on the value to be rounded. Expected results: Decimal rounding should always behave like the ES5 Standard. This includes small integers.
To help me prioritize this, is this a just a spec variance that you happened to notice, or do you already have an application that gets bitten by it?
Status: UNCONFIRMED → NEW
Ever confirmed: true
It's just a spec variance that has been around for years. I don't suppose it hurts anyone, other than give them a surprise if they want well-defined decimal rounding.
(In reply to roger.andrews@mail104.co.uk from comment #2) > It's just a spec variance that has been around for years. > > I don't suppose it hurts anyone, other than give them a surprise if they > want well-defined decimal rounding. Thanks. I assigned it medium priority.
Quick note: It seems that David Gay's algorithms for binary->decimal were designed for "round to nearest, ties to even" (Bankers' Rounding), according to page 19 of William Kahan's 1997 Lecture Notes on IEEE754: http://www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF Perhaps dtoa.c uses this blindly, without adapting for ES5's "ties away from 0".
Assignee: general → nobody
Severity: normal → S3
Status: NEW → RESOLVED
Closed: 2 years ago
Resolution: --- → INACTIVE
You need to log in before you can comment on or make changes to this bug.