Bug 24711
| Summary: | 1.95.toFixed(1) should equal "1.9" but does not | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Jeff Walden (remove +bwo to email) <jwalden+bwo> |
| Component: | JavaScriptCore | Assignee: | Nobody <webkit-unassigned> |
| Status: | RESOLVED DUPLICATE | ||
| Severity: | Normal | CC: | apeller, barraclough, brendan, bugzilla33, emacemac7, gavin.sharp, mrbkap, zwarich |
| Priority: | P2 | Keywords: | HasReduction, InRadar |
| Version: | 528+ (Nightly build) | ||
| Hardware: | All | ||
| OS: | All | ||
| URL: | javascript: alert(1.95.toFixed(1) === "2.0" ? "FAIL" : "PASS") | ||
Jeff Walden (remove +bwo to email)
https://bugzilla.mozilla.org/show_bug.cgi?id=484293
This is reproducible in Safari 4 beta on 10.4 (r41760) and on Windows XP (just 4b). May you have as much pleasure dealing with this as I did in (incorrectly) checking SpiderMonkey's behavior, because it would be the height of unfairness for me to hog all the fun. :-)
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Mark Rowe (bdash)
<rdar://problem/6702946>
Cameron Zwarich (cpst)
I don't understand why 1.9 is the correct value. The ECMA spec gives an algorithm for toFixed in 15.7.4.5. This algorithm is copied almost literally by the implementation in JavaScriptCore. I'll double check this case by hand.
Jeff Walden (remove +bwo to email)
Cameron, see the Mozilla bug, it's all calculated by hand there -- just the problem of a flipped equality. But if you do feel like running it out by hand, by all means feel free, because you'll get an even better idea of how much fun can be had here.
Alexey Proskuryakov
See also: bug 16651, bug 16653.
Cameron Zwarich (cpst)
Okay, I see our mistake. This will be a bit of work to fix. There may be similar problems in other places.
bugzilla33
toFixed rounds, not floors !
bug 24711 is not a Bug !!
(1.95) . toFixed( 1 ) = 2.0
because:
Math.round ( 1.95 * 10 ) / 10
equal 2
bugzilla33
Not a Bug.
All browsers before returns string value (IE, Opera, Konqueror, Chrome, Firefox) rounding a Number to defined scale.
Gavin Barraclough
For a simpler explanation of why this is a valid bug, try:
javascript:alert((1.95).toPrecision(21))
(but! this also doesn't work in ToT!) - the correct result should be 1.94999999999999995559.
Since the precise mathematical value passed to toFixed is closer to 1.9 than 2.0, 1.9 is the correct result.
This should be fixed by https://bugs.webkit.org/show_bug.cgi?id=44745
*** This bug has been marked as a duplicate of bug 44745 ***
Gavin Barraclough
*** Bug 31777 has been marked as a duplicate of this bug. ***