Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does using toString() on a float give different results in Firefox vs Chrome vs IE?

If you run this code in Firefox and Chrome, it will output a different converted string.

(0.1234).toString(36)

In Firefox it will return a value of "0.4fxcm49g2j8"

In Chrome or Node 4+ (likely a V8 difference), I get a value of "0.4fxcm49g2j91m31w5nq6ldte29".

In IE and Edge it is "0.4fxcm49g2j91" (thanks @JaromandaX)

Anyone know why? Just curious.

like image 281
Cameron Avatar asked Oct 17 '22 21:10

Cameron


1 Answers

It seems like Firefox will cut the string after max_chars (which is different for every representation base) while Chrome doesn't.

I didn't find any valid reference in the MDN, but if you check (for example) the same for radix=35:

Firefox: "0.4b5r4d4d4d4"
Chrome:  "0.4b5r4d4d4d4ar1synaijrkxa67ojifsn1fyljg8bny4ix53gyasaqys92cxcprnbsg27hs9ht7wb3o3i0ibfu9wvlyykt2evoykvyrva0n0wotvvtjxxnc98bptr68t77wl74xpwsf0iui8gtkhw45c9bhxkw8q6ltnpjti9110jqhdfxn3572gy3gsg74j7b55ids0vyfldmp2m4q32a7m1bf392ag1cppfwt1hvs57ne98v1ep5pa8un6bymql44dv1b6digg657rl1t82myanxb2q3nwf20vk1rl3rlylnbxansyhyuhie6pl1kmsqdhqhkmoqbanpj0d2vyiqxao70qrgarpyw7hdkdlly2048jx1f9ojl8417lr7j7f33d74r2bycapvhuq5vxr869r433q5rip9kpe7wft319p211af7x3v28f3ibto7s3c7rpg53kfbhtt23rta17fqw3653psp1febq2veigemernc4q85hjwx86awmb0npiqnm3f4994c9pu02bbn0nde9fnaqnmx68yp0q1itvc4jisckdtf08d26k4oha3lsh6g26o9pfoppwyrtbqgdpav2i2k0dhucraowuuoysx9h4ii3ht4qx9snj5w1k7nxnpspstd56dw8efbah6ly1km21g7nntn4sxup3fan1j576jbh02w1ixr5xivir0npqfdykndh5tcr32n6jt5lpe7a1u7efqkpdjgkwtmi3k2bcs4nuvkp6o5ds9l9biuw9td0ofhtgyffgyu5w4twliug2mkmpy9lk8m1pk211gfiwvutp5t0qfclfo0in7b3nml3s578aybv9yovwwvwn428bvuitux4lfgxenv93jp2dovf3fr73nhxrqik7u086crrtq7gm1xqw2i5ikkr8cxjyufvehpyqebse3ioex82yqn835p8g2xriidikp96swm2vfjwwdt54xurj2ufhdy0b805gixv9vrwcuf2rd56dtkgonpipcswbg1cjfxddbwlxwmew0xymmfv972enwa0wjuakcd2fswl4e4794bs7gqvcgaksxh21tjs74r9b9rwun4x8osk"

You can check this in all browsers:

for (var i = 11; i <=36; i++) {
  console.log(i, (0.1234).toString(i).length, (0.1234).toString(i));
}

In chrome the max lenght I got was 1101 while in firefox it was 19.

It looks like some kind of rounding when converting the numbers between bases, however I'm not sure sure which/when/what round firefox applies here.

like image 151
Dekel Avatar answered Nov 15 '22 08:11

Dekel