From: Benjamin 'BeRo' Rosseaux on
Am 21.06.2010 22:50, schrieb Scott Sauyet:
> I saw the exponent code, and am not quite sure how that should play
> with bases higher than 14 (it gets tricky when you consider that
> "1.0e3", base 10, should convert to 1000 but "1.0e3", base 16, should
> convert to 1.055419921875. The question is what does "e" represent?

"e" is the exponent char at least for base 10, which will can generate
at least at "9.8.1 ToString Applied to the Number Type" in the ES5 spec.
At radix larger than 14, the "e" exponent char will ignored, and at
radix larger than 25, the "p" exponent char will ignored.

> But do you foresee a need for multiple consecutive minus/plus signs?

And the other with the multiple minus/plus signs is just for to be
foolproof-safe, and can be changed easy, if needed.






From: Scott Sauyet on
Benjamin 'BeRo' Rosseaux wrote:
> Am 21.06.2010 22:50, schrieb Scott Sauyet:
>> I saw the exponent code, and am not quite sure how that should play
>> with bases higher than 14 (it gets tricky when you consider that
>> "1.0e3", base 10, should convert to 1000 but "1.0e3", base 16, should
>> convert to 1.055419921875.  The question is what does "e" represent?
>
> "e" is the exponent char at least for base 10, which will can generate
> at least at "9.8.1 ToString Applied to the Number Type" in the ES5 spec.
> At radix larger than 14, the "e" exponent char will ignored, and at
> radix larger than 25, the "p" exponent char will ignored.

This might be the best approach, but it certainly feels awkward.

In any case, your approach is similar to the one I posted. Yours
iterates forward over the fractional digits, versus mine which went
backwards. It seems to gain the same additional precision over other
approaches that mine does. But it doesn't round badly on 0.fgr, base
36 as mine did. Definitely a nice version. I'm wondering if it will
round badly on other short strings, but I haven't done any serious
tests.

In a separate post, I will detail a slightly different approach I've
been trying which is intended to clean up the likely (although
untested) performance problems in my earlier version and avoid the
rounding errors with shorter strings.

--
Scott
From: Scott Sauyet on
Scott Sauyet wrote:
> Dr J R Stockton wrote:
>> 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.

I've found a little time finally, and the new approach definitely
seems an improvement, both in the actual results and the likely
performance considerations.

I no longer lazily load the regexes needed, as I have to save some
other values for each base and it made sense just to compute them
together. I keep a list of the powers of each base less than or equal
to 2^53. I still have a separate parseFraction function, but instead
of working digit-by-digit, it calls parseInt on chunks of as many
digits as will fit inside 2^53.

It's not well-tested yet, but I've posted it on my latest test page,
here:

<http://scott.sauyet.com/Javascript/Test/2010-06-21b/>

The PHP source is at

<http://scott.sauyet.com/Javascript/Test/2010-06-21b/index.phps>

Here is the function:

var parseFloat_ss2 = (function() {
var
origPF = parseFloat,
DEPTH = 2,
maxInt = Math.pow(2, 53),
allDigits = "0123456789abcdefghijklmnopqrstuvwxyz",
regexes = [null, null],
powers = [null, null],
parseFraction = function(str, base) {
if (!str) return 0;
var basePowers = powers[base],
max = basePowers[basePowers.length - 1];
total = 0,
denominator = 1,
count = 0;
while (str.length && count < DEPTH) {
var index = Math.min(str.length, basePowers.length - 1);
total += parseInt(str.substring(0, index), base) /
(denominator * basePowers[index]);
str = str.substring(index);
denominator *= max;
count++;
}
return total;
};
(function() {
for (var base = 2; base <=36; base++) {
var digits = allDigits.substring(0, base);
regexes[base]= new RegExp("(^[\\-\\+]?)([" + digits +
"]*)(?:\\.([" + digits + "]*))?$", "i");
powers[base] = [];
var power = 1, i = 0;
while (power <= maxInt) {
powers[base][i] = power;
power *= base;
i++;
}
}
}());
return function (str, base) {
if (!base || base == 10) return origPF(str);
if ((base < 2) || (base > 36) || (base % 1)) return NaN;
var regex = regexes[base],
match = regex.exec(str);
if (!match) return NaN;
return ((match[1] == "-") ? -1 : 1) * (
(match[2] == "" ? 0 : parseInt(match[2], base)) +
parseFraction(match[3], base)
);
};
}());

--
Scott
From: Dr J R Stockton on
In comp.lang.javascript message <7550eef3-42f2-4d97-9459-dea2356aa84b(a)z8
g2000yqz.googlegroups.com>, Mon, 21 Jun 2010 13:50:01, Scott Sauyet
<scott.sauyet(a)gmail.com> posted:

>I saw the exponent code, and am not quite sure how that should play
>with bases higher than 14 (it gets tricky when you consider that
>"1.0e3", base 10, should convert to 1000 but "1.0e3", base 16, should
>convert to 1.055419921875. The question is what does "e" represent?

In a browser, a full function parseFloat(String, radix) should accept
the exponent format which, in that browser, toString(radix) gives for
sufficiently large and small Numbers. But it should accept that format
for all finite values, and with the radical point not in the same
position.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.
From: Dr J R Stockton on
In comp.lang.javascript message <703e1d78-8204-4c60-b73f-eac7de238028(a)j4
g2000yqh.googlegroups.com>, Mon, 21 Jun 2010 12:51:53, Scott Sauyet
<scott.sauyet(a)gmail.com> posted:

>Dr J R Stockton wrote:
>> 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.
>
>I don't understand the first part of that. What browser has the
>button covering something important?

The button was covered, in part, by the dropped list.

> Does the latest version fix the
>UI versions you mention?
>
> <http://scott.sauyet.com/Javascript/Test/2010-06-21a/>

Yes. But you set the width to 800px.

I have a 1280*1024 screen which is a little bigger than A4 landscape.
But I like to be able to work with an editor (A5 portrait) on one side
of the screen and material-to-read in A5 portrait on the other side.
That means that my browser is normally set by
<input type=button value=ReSize
onClick="window.resizeTo(640, window.screen.height*0.85)">
in its home page; and, for browsers where resize does not work, that
page has a fixed-width Table between a pair of <hr> so I pull the side
of the window until the lines and table match.

Code which I edit gets a default 72-character line marking; I write a
little shorter so that it fits into my displays, and then of course it
fits News.


In Opera, one can edit the source copy. You page is nearly OK with min-
width 300 px and actual width 640 px ; a slight reduction in margin or
padding would make it OK.

I'm not, of course, saying that you should set 640 px width; just that
with an unspecified width and economy of margins a reader gets more
choice.


Where you have more than one function from a person, I suggest that the
first be numbered "1". My ExactPF us now called BetterPF for two
reasons, one being that it is not invariably exact.

--
(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.