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: Thomas 'PointedEars' Lahn on 30 May 2010 07:30 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? > 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: var s = (-Math.PI/10).toString(16); var i = parseInt(s, 16); var f = (s.match(/\.([\da-f]{1,198})/i) || [, "0"])[1]; var n = i + (s.charAt(0) == "-" ? -1 : 1) * parseInt(f, 16) / Math.pow(16, f.length); But if `s' would be user-defined, it could have leading whitespace, so: var n = i + (/^\s*-/.test(s) ? -1 : 1) * parseInt(f, 16) / Math.pow(16, f.length); PointedEars -- Use any version of Microsoft Frontpage to create your site. (This won't prevent people from viewing your source, but no one will want to steal it.) -- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
From: Ry Nohryb on 30 May 2010 10:28 On May 30, 1:30 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > (...) > > var s = (-Math.PI/10).toString(16); > var i = parseInt(s, 16); > var f = (s.match(/\.([\da-f]{1,198})/i) || [, "0"])[1]; ___________________________________^^^____________________ There's no need to trim f to 198 chars: Math.pow(16, n) overflows at n === 256, not @ 199. -- Jorge.
From: Thomas 'PointedEars' Lahn on 30 May 2010 14:57 Ry Nohryb wrote: > Thomas 'PointedEars' Lahn wrote: >> (...) >> >> var s = (-Math.PI/10).toString(16); >> var i = parseInt(s, 16); >> var f = (s.match(/\.([\da-f]{1,198})/i) || [, "0"])[1]; > ___________________________________^^^____________________ > > There's no need to trim f to 198 chars: Not in this example, but for the general case without an adaptive algorithm. > Math.pow(16, n) overflows at n === 256, not @ 199. But parseInt(s, 36) "overflows" at more than 198 consecutive "z"s in s. PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
From: Ry Nohryb on 30 May 2010 16:15 On May 30, 8:57 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de> wrote: > Ry Nohryb wrote: > > Thomas 'PointedEars' Lahn wrote: > >> (...) > > >> var s = (-Math.PI/10).toString(16); > >> var i = parseInt(s, 16); > >> var f = (s.match(/\.([\da-f]{1,198})/i) || [, "0"])[1]; > > ___________________________________^^^____________________ > > > There's no need to trim f to 198 chars: > > Not in this example, but for the general case without an adaptive algorithm. > > > Math.pow(16, n) overflows at n === 256, not @ 199. > > But parseInt(s, 36) "overflows" at more than 198 consecutive "z"s in s. The fractional part can never be > ( the divisor -1 ), that's why you've got to look at the divisor (Math.pow(base, len) instead, as I did in my "take 3", and adjust len accordingly (to both the base and the fractional part string length): /* Tested in Safari, Chrome, Opera & FF */ String.prototype.toFP= function (base, n, r, w, div) { if (/[^0-9a-z\.+-]/i.test(this)) return NaN; n= this.split('.'); if (isFinite(r= parseInt(n[0], base))) { if (w= (n= n[1]).length) { /*trim until it's finite*/ while (!isFinite(div= Math.pow(base, w))) w--; r+= (r<0 ? -1:1)* parseInt(n.substr(0, w), base)/ div; } } return r; }; for (var base=2; base <37; base++) console.log([base, Math.PI.toString(base).toFP(base)]) --> === Math.PI -- Jorge.
From: Dr J R Stockton on 30 May 2010 12:28
In comp.lang.javascript message <Xns9D87B7820AFF3eejj99(a)194.109.133.242> , Sat, 29 May 2010 16:02:20, Evertjan. <exjxw.hannivoort(a)interxnl.net> posted: >Dr J R Stockton wrote on 28 mei 2010 in comp.lang.javascript: > >> In comp.lang.javascript message <Xns9D85CE4464FCFeejj99(a)194.109.133.242> >> , Thu, 27 May 2010 18:16:34, Evertjan. <exjxw.hannivoort(a)interxnl.net> >> posted: >However we were [or at least I was] just contemplating a general function >for conversion of any root[1..36] floating point string to number value. Remember to include isNaN & !isFinite testing. >Such functions are much safer explicit than embedded, Sometimes. The advantage of an embedded bug is that someone else is more likely to find it first, and get it fixed. Opera 10.10, but not Opera 10.53 : Number.toString(radix) ignores radix. >btw: in root 1 only the zero value can be expressed. Eh? -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links; Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. |