R
Ry Nohryb
(...) you are calling the callback futilely at least 886 times in
V8 because precision does not suffice to represent the factor (...)
[*] FIXED:
String.prototype.toFP= function (base, d, w, n, r, s, pw) {
if (/[^0-9a-z\.+-]/i.test(this)) return NaN;
d= "0123456789abcdefghijklmnopqrstuvwxyz";
s= (r= parseInt((n= this.split('.'))[w= 0], base)) < 0 ? -1 : 1;
n= n[1].toLowerCase().split('');
while(n.length && (pw=Math.pow(base,--w))) r
+=s*d.indexOf(n.shift())*pw;
return r;
};
for (var base=2; base <37; base++) console.log([base,
Math.PI.toString(base).toFP(base)])
-->
[2, 3.141592653589793]
[3, 3.141592653589794]
[4, 3.141592653589793]
[5, 3.1415926535897936]
[6, 3.141592653589793]
[7, 3.141592653589794]
[8, 3.141592653589793]
[9, 3.141592653589794]
[10, 3.141592653589793]
[11, 3.141592653589793]
[12, 3.141592653589793]
[13, 3.141592653589793]
[14, 3.141592653589793]
[15, 3.1415926535897936]
[16, 3.141592653589793]
[17, 3.141592653589793]
[18, 3.141592653589794]
[19, 3.1415926535897922]
[20, 3.141592653589793]
[21, 3.1415926535897936]
[22, 3.1415926535897927]
[23, 3.141592653589793]
[24, 3.141592653589793]
[25, 3.1415926535897936]
[26, 3.141592653589793]
[27, 3.1415926535897927]
[28, 3.141592653589794]
[29, 3.141592653589793]
[30, 3.141592653589793]
[31, 3.141592653589793]
[32, 3.141592653589793]
[33, 3.141592653589793]
[34, 3.141592653589793]
[35, 3.141592653589793]
[36, 3.141592653589793]