Error in number.toString conversion in (irregular) base
Reported by
bcrie...@gmail.com,
Oct 24 2016
|
||||||
Issue descriptionUserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36 Steps to reproduce the problem: 1. open console 2. (0.19155912401454667).toString(36) 3. "0.6w9drpgd1345y0p63ovj10pb9" What is the expected behavior? "0.6w9drpgd134" A numberstring in base 36 cannot be longer then a numberstring in base 10 Radix argument should be between 2-36 `Uncaught RangeError: toString() radix argument must be between 2 and 36` Also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString What went wrong? Some fundamental arithmetic ;) Try (0.19155912401454667).toString(11): "0.211a68745184085717213743862504511a687827188026678389aa0972798272a354303a4687548020541a076282927a62770387aa3632677a722aa43601a553973571a3296553520a41135261703237a47100908676739901063003153a62060660570859a6826458614aa720140a563036727688856608978016a690090738809a74947577993413333a1856a4a75556524158371816153326563111991972205714aa8542877a4550577055736766102703434697629499000305a9297953184a17a92822a319955a837025a34a005084095567257828a6420863a3705352433248307582907695857079169364885778783a736033aa517a3510208a1140097718535590354978151147342270931592a0a74005479203453945a31a999843a12059812601156247060268926980726273aa2065880599a1346a670a8579a6441196846865515934aa1309a187783a53a8a40a5a1794a89853890a1207443932436a0626245a6245724004295764a7a6073a255573958916a366a739442a63055687988498345632661643661979197144a87a202852568814038985664362a30345171586aa0764902698a1a7950509a2a44119a6369626560937535654796981810133745688aa4 Doesn't make sense to me. Did this work before? Yes Not sure Chrome version: 53.0.2785.143 Channel: stable OS Version: Ubunut 16.04 Flash Version: Shockwave Flash 11.2 r999 Quite sure this error wasn't in V8 a (while/some months/years) ago.
,
Oct 26 2016
Tested the issue on chrome Stable #54.0.2840.71, Canary 56.0.2900.0 in Ubuntu 14.04 and was able to reproduce the issue. This is a Non-Regression issue since seeing this from M35 #35.0.1898.0, Making the status to Untriaged so that the issue would get addressed. Note : Able to reproduce the issue in MAC 10.12 and Windows 10.0 Thank you.
,
Oct 27 2016
On second thought, I'm not sure if this is really a bug. For fractions, a numberstring in base 36 can be longer then a numberstring in base 10. Like 3 + 1/3 in base 3 = 10.1 while in base 10 it can only be estimated (3.33333) It's probably a (not) rounding issue, a behavior that differs from Firefox and other JS engines.
,
Nov 10 2016
,
Nov 22 2016
Is this something that works according to spec?
,
Nov 22 2016
This is not a bug. 4/3 in base 3 is indeed 1.1. However, Javascript uses IEEE 754 floating point numbers, which are base 2. Storing 1/3 in base 2 will experience rounding errors. The spec (20.1.3.6 Number.prototype.toString, step 7) says: Return the String representation of this Number value using the radix specified by radixNumber. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-dependent, however the algorithm should be a generalization of that specified in 7.1.12.1. So differing from Firefox does not surprise me.
,
Nov 22 2016
Still the outcome of 0.19155912401454667).toString(11) does suggest a significance or precision that isn't there. (919 significant figures) Isn't that a bug?
,
Nov 22 2016
This issue sounds very similar to Issue 666376 . I don't think there's a spec violation (and one certainly should not read significance into IEEE float digits printed!), but it's possible that the Firefox results are subjectively "better" for users. Safari also outputs the shorter string.
,
Nov 24 2016
The following revision refers to this bug: https://chromium.googlesource.com/v8/v8.git/+/21b0dbedfd470b67c47f84428e1af95a506e49e5 commit 21b0dbedfd470b67c47f84428e1af95a506e49e5 Author: yangguo <yangguo@chromium.org> Date: Thu Nov 24 10:30:19 2016 Reimplement Number.prototype.toString with non-default radix. The old algorithm produces unnecessary decimal digits. The new one converts the significand of the input double into an uint64_t to be just as precise as necessary. R=tebbi@chromium.org BUG= chromium:658712 , chromium:666376 Review-Url: https://codereview.chromium.org/2520363002 Cr-Commit-Position: refs/heads/master@{#41255} [modify] https://crrev.com/21b0dbedfd470b67c47f84428e1af95a506e49e5/src/builtins/builtins-number.cc [modify] https://crrev.com/21b0dbedfd470b67c47f84428e1af95a506e49e5/src/conversions.cc [modify] https://crrev.com/21b0dbedfd470b67c47f84428e1af95a506e49e5/test/mjsunit/number-tostring.js [modify] https://crrev.com/21b0dbedfd470b67c47f84428e1af95a506e49e5/test/webkit/fast/js/number-toString-expected.txt [modify] https://crrev.com/21b0dbedfd470b67c47f84428e1af95a506e49e5/test/webkit/fast/js/toString-number-expected.txt
,
Aug 8 2017
|
||||||
►
Sign in to add a comment |
||||||
Comment 1 by e...@chromium.org
, Oct 24 2016