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 19 Jun 2010 08:52 On Jun 19, 6:01 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > (...) Would you mind to update in your page my code to the latest version ? It is: String.prototype.toFP= (function (regExpCache) { /* 20100531, by jo...(a)jorgechamorro.com */ return function (base, n, r, w, div) { if ((base < 2) || (base > 36) || (base % 1)) return NaN; if (!(n= regExpCache[base])) { n= "0123456789abcdefghijklmnopqrstuvwxyz".substr(0, base); n= "^\\s{0,}([-+]{0,1})(["+n+"]{0,})[.]{0,1}(["+n+"]{0,})\ \s{0,}$"; regExpCache[base]= n= new RegExp(n, "i"); } if (!(n= n.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; }; })([]); TIA, -- Jorge.
From: Dr J R Stockton on 19 Jun 2010 18:47 In comp.lang.javascript message <4StvyxiTGAGMFw9Q(a)invalid.uk.co.demon.me rlyn.invalid>, Tue, 15 Jun 2010 23:41:23, Dr J R Stockton <reply1023(a)merlyn.demon.co.uk> posted: > >function parsFlotB(S, R) { > S = S.replace(/(\d*\.)/, "0$1") // ensure \d+ before point > var A = parseInt(S, R) > if (S = S.split(".")[1]) { var NR = 1, L = 0 > S = S.substring(0, 99) // Crude partial fix for excess length > while (1+parseInt(S.charAt(L++), R)) NR *= R // good digits > A += (1/A>0?+1:-1) * parseInt(S, R) / NR } > return A } REPLACE \d with \w. Also, parsFlotC is in hand. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)
From: Scott Sauyet on 19 Jun 2010 21:19 Ry Nohryb wrote: > On Jun 19, 6:01 am, Scott Sauyet <scott.sau...(a)gmail.com> wrote: > Would you mind to update in your page my code to the latest version ? Not at all. It's in the latest version, here: <http://scott.sauyet.com/Javascript/Test/2010-06-19a/> If you have PHP available and want to host your own version, the PHP is here: <http://scott.sauyet.com/Javascript/Test/2010-06-19a/index.phps> -- Scott
From: Scott Sauyet on 19 Jun 2010 21:22 Dr J R Stockton wrote: > REPLACE \d with \w. Done. It's in the latest version, posted here: <http://scott.sauyet.com/Javascript/Test/2010-06-19a/> > Also, parsFlotC is in hand. I look forward to it. I need to find a little time for my next approach, but I do have a new idea as well. -- Scott
From: Dr J R Stockton on 20 Jun 2010 13:39
In comp.lang.javascript message <1ba54467-b1ec-423a-9989-4eb3b3288fec(a)a3 0g2000yqn.googlegroups.com>, Fri, 18 Jun 2010 21:01:35, Scott Sauyet <scott.sauyet(a)gmail.com> posted: >Dr J R Stockton wrote: >> function ExactPF(S, Rdx) { var J, L = 0, R = 0, RN = 1 >> � S = S.split(".") >> � var Int = S[0].split("") >> � var Frc = S[1].split("") >> � var Sgn = Int[0] == "-" ? -1 : +1 >> � if (Sgn == -1) Int.shift(1) >> � for (J = 0 ; J < Int.length ; J++) >> � � L = L * Rdx + parseInt(Int[J], Rdx) >> � for (J = 0 ; J < Frc.length ; J++) { RN *= Rdx >> � � R = R * Rdx + parseInt(Frc[J], Rdx) } >> � return Sgn * ( L + R/RN ) } // Consider case of L or R exceeding 2^53 >> >> Note that it uses parseInt only on single characters, which reduces the >> chance of error. �I've nor found any; but it is practical to test >> parseInt with all bases and all single-character strings, but not with >> all multi-digit strings. > >I've included it on the page without considering it thoroughly >enough. I'll try to have another look over the weekend. ExactPF now does not use parseInt. And it has been demoted (in <URL:http://www.merlyn.demon.co.uk/js-maths.htm#pF>) to be called BetterPF. Routines should be tested, base 10, with 0.999999999999999999999999999 for a varying number of nines and similar on other bases. A result of over 1.0 is bad. Consider putting the test button somewhere that the "value" does not cover when showing a list, and putting the results below, rather than beside, the input. For production code, ISTM OK to use the default parseFloat for base 10. On the other hand, that assumes it to be right. But for test ing conversion code, it is well to have base 10 done in the same way as other bases, enabling a meaningful comparison with parseFloat. Function parseFloat should not be used for currency input, since it inevitably gives inexact Numbers; instead, one should check the format with a RegExp match and construct the exact number of pennies (does not apply to much Government work). Otherwise, the nature of the following calculation is probably such that an LSB or two of error does not matter; routines such as we have seen are OK (but one might add handling of exponents such as toString gives). But, for test purposes, something perfectly accurate at all times is needed (with rounding into the LSB defined for the half-way case). -- (c) John Stockton, near London. *@merlyn.demon.co.uk/?.?.Stockton(a)physics.org Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Correct <= 4-line sig. separator as above, a line precisely "-- " (RFC5536/7) Do not Mail News to me. Before a reply, quote with ">" or "> " (RFC5536/7) |