Prev: Firefox cache issue
Next: FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? (2010-05-27)
From: Ry Nohryb on 31 May 2010 13:09 On May 31, 6:54 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> wrote: > Stefan Weiss wrote on 31 mei 2010 in comp.lang.javascript: > > > Since you like short code: > > > {0,} is the same as * > > {1,} is the same as + > > and {0,1} is ? That looks like a question, hahaha. -- Jorge.
From: Ry Nohryb on 31 May 2010 13:23 On May 31, 4:55 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > (...) > /* get the digits that are valid for this base */ > validDigits= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); > (...) Oops, forgot to declare "validDigits". What if I just reuse "n" again ? String.prototype.toFP= function (base, n, r, w, div) { //20100531 by jorge(a)jorgechamorro.com /* check that base is in range and an integer */ if ((base < 2) || (base > 36) || (base % 1)) return NaN; /* get the digits that are valid for this base */ n= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); /* validate structure and contents of the input str : */ /* ^ (optional) whitespace + (optional) a single char [-+] */ /* + (optional) 0 or more validDigits + (optional) a point */ /* + (optional) more validDigits + (optional) whitespace $ */ n= "^\\s{0,}([-+]{0,1})(["+ n+ "]{0,})[.]{0,1}(["+ n+ "]{0,})\ \s{0,}$"; /* exec n on 'this' now, case-insensitively, and reuse n*/ if (!(n= new RegExp(n, "i").exec(this))) return NaN; /* by now we've got captured, cleaned-up and validated : */ /* n[1]= sign, n[2]=integer part, n[3]= fractional part */ if (isFinite(r= parseInt(n[2] || "0", base)) && (w= n[3].length)) { /* trim until div is finite */ while (!isFinite(div= Math.pow(base, w))) w--; r+= parseInt(n[3].substr(0, w), base)/ div; } /* sign is one of "1" or "+1" or "-1" */ return (n[1]+ "1")* r }; "\t -.z\r".toFP(36).toString(36) --> "-0.z" -- Jorge.
From: Ry Nohryb on 31 May 2010 13:47 On May 31, 7:23 pm, Ry Nohryb <jo...(a)jorgechamorro.com> wrote: > > (...) W/o the comments it's really just only 11 LOCs : String.prototype.toFP= function (base, n, r, w, div) { if ((base < 2) || (base > 36) || (base % 1)) return NaN; n= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); n= "^\\s{0,}([-+]{0,1})(["+n+"]{0,})[.]{0,1}(["+n+"]{0,})\\s{0,}$"; if (!(n= new RegExp(n, "i").exec(this))) return NaN; if (isFinite(r= parseInt(n[2] || "0", base)) && (w= n[3].length)) { while (!isFinite(div= Math.pow(base, w))) w--; r+= parseInt(n[3].substr(0, w), base)/ div; } return (n[1]+ "1")* r; }; What other bugs are there left into it ? Would it be a good idea to memoize the regExps ? Hey, Pointy, what's its Jorgtropy level ? -- Jorge.
From: Dr J R Stockton on 31 May 2010 13:34 In comp.lang.javascript message <3459374.heAe9J7NaK(a)PointedEars.de>, Sun, 30 May 2010 13:30:58, Thomas 'PointedEars' Lahn <PointedEars(a)web.de> posted: >Dr J R Stockton wrote: > >> Thomas 'PointedEars' Lahn posted: >>> Yes, good catch; we need to consider the sign with addition, e.g.: >>> >>> var s = (-Math.PI).toString(16); >>> var i = parseInt(s, 16); >>> var f = (s.match(/\.([\da-f]+)/i) || [, "0"])[1]; >>> var n = i + (i < 0 ? -1 : 1) * parseInt(f, 16) / Math.pow(16, f.length); >> >> You need to consider it more effectively, and to test adequately. > >Do you know what "quick hack" means? Generally slovenly working, with a tendency to miss the obvious. >> That indeed gives -3.141592653589793; but use instead Math.PI/10 and it >> gives 0.3141592653589793. Whenever parseInt(s, 16) gives a zero, your >> code will give a positive result. > >ACK, thanks. ISTM that checking whether the first character of the >representation is a `-' solves this particular problem. Again, largely >untested: Entirely unnecessary. Just use the sign of the number 'i'. Unless you are working with dates, it is better to use j, rather than i, for a short-term identifier in News. The former is likely, in an unknown font, to be better distinguishable from other characters; and it does not excite the attention of a spelling-checker. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME Prof Timo Salmi's Usenet Q&A <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqn.zip> TS FAQs via : http://www.uwasa.fi/~ts/http/ : tsfaq.html quote margin &c. Jukka Korpela: <URL:http://www.malibutelecom.com/yucca/usenet/dont.html>.
From: Ry Nohryb on 31 May 2010 18:51
On May 31, 7:34 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk> wrote: > In comp.lang.javascript message <3459374.heAe9J7...(a)PointedEars.de>, > > >ACK, thanks. ISTM that checking whether the first character of the > >representation is a `-' solves this particular problem. Again, largely > >untested: > > Entirely unnecessary. Just use the sign of the number 'i'. i would be zero. What's the sign of zero ? -- Jorge. |