Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why toString() of a number returns a longer string in Chrome 49?

I just noticed that Math.random().toString(36) returns a longer string in Chrome 49 than Node 5.9 or Safari 9.1.

Safari

> Math.random().toString(36)
< "0.ivmamr8qidc" = $1
> Math.random().toString(36).length
< 12 = $2

Node 5.9.1

> process.version
'v5.9.1'
> Math.random().toString(36)
'0.efjsrk5vrucsdcxr'
> Math.random().toString(36).length
18

Chrome 49

> 0.05986301500440483.toString(36)
"0.25kyvln343abt9usp7thdunmi"
> Math.random().toString(36).length
28

enter image description here

Note: This is not about Math.random. Any number when converted to string with any radix(base) other than 10 returns a much larger string in Chrome 49 than Safari:

Safari

> 0.3.toString(11)
< "0.3333333333333333" = $3

Chrome

> 0.3.toString(11)
"0.33333333333333326157299268964aa565025a1144557416824326113516284528415692759737689558662a213684953041133490a150361056078529388624a6a95140066041152159194505728108268409453322a67850a92802aa020416578a21776a17a9597a008942a33344a987a722818a995915441aaa138356487483041a8a037765398541258a129423750a196266713086883273a32338000a969744a80121651a43094a436447466473193012044680270753658119189443226363898951aaa2609388078443a45888122a267a250699383701744a42a46740085019817923897897064746376425100961478677938aa1362825a2aa025317275a462740a615586933734946aa696449358972158a81891a247783970290317a2a3850658423343543634788830516939282a0621473619a878519a550102aa63989123430969406321554746982403894531491854919a5131341277902253a163136367215309647141444a6613a4999717880973a9626a90139a747188035a4565448938098960277046602486a2670a480090985a833179882708959744343031053aa84003051a7aa213409a052287587859372843879a77419001a1034828962548225876270737363a2590173794295a692243041469261a1468303153492137a6576155a276a5272a3807220204415092553212a020403960a1838347778678826885246195448a77419353738508aa3089972946871786aa811658725243984a"

What's going on?!

like image 484
Mohsen Avatar asked Mar 29 '16 01:03

Mohsen


1 Answers

I'm convinced, after testing this out a little bit, that its a bug in chrome for certain bases, the most common ones seem to be OK, while uncommon bases are sometimes just ridiculous in length. Here is a quick JSBin with some logging of various lengths:

JSBin - (try in various browsers)

var a = Math.random().toString(2).length; // 53 (doesn't totally jive with my theory) 
var b = Math.random().toString(8).length; // 19
var c = Math.random().toString(10).length; // 18
var d = Math.random().toString(16).length; //15

var e = Math.random().toString(4).length; // 28
var f = Math.random().toString(11).length; // 1101 ?
var g = Math.random().toString(36).length; // 27
var h = Math.random().toString(3).length; // 1101 ?
var i = Math.random().toString(17).length; // 1101 ?
var j = Math.random().toString(32).length; // 13
var k = Math.random().toString(12).length; // 28

Further more from the ECMA 5.1 spec we have this:

.... The precise algorithm is implementation-dependent if the radix is not 10, however the algorithm should be a generalisation of that specified in 9.8.1.

ECMA 5.1 Spec - link to relevant part

I really don't know what else could be going on here. But this is beginning to look more like a algorithmic issue with chrome's specific implementation.

*pardon my variables, its only for logging in the JSBin.

like image 128
omarjmh Avatar answered Oct 22 '22 07:10

omarjmh