From: Dr J R Stockton on
In comp.lang.javascript message <1790020.adu78ljVUC(a)PointedEars.de>,
Tue, 1 Jun 2010 17:58:37, Thomas 'PointedEars' Lahn <PointedEars(a)web.de>
posted:

>Dr J R Stockton wrote:
>
>> Thomas 'PointedEars' Lahn posted:
>>> Dr J R Stockton wrote:

>>>> 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'.
>
>I had: (i < 0 ? -1 : 1). But *you* pointed out to me that `i' would be 0
>if the absolute value of the represented number would be less than 1.

Indeed you did; and, as you know, your test does not suffice, since it
does not always give the sign of the number 'i'.

I repeat - you need to use the sign of the number 'i'.

You so frequently insist that others search the archives; you should do
so yourself. The matter was referred to sometime in the years 200x.
Careful reading of ECMA 262 3/5 could help you.

--
(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: Thomas 'PointedEars' Lahn on
Dr J R Stockton wrote:

> Thomas 'PointedEars' Lahn posted:
>> Dr J R Stockton wrote:
>>> Thomas 'PointedEars' Lahn posted:
>>>> Dr J R Stockton wrote:
>>>>> 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'.
>> I had: (i < 0 ? -1 : 1). But *you* pointed out to me that `i' would be 0
>> if the absolute value of the represented number would be less than 1.
>
> Indeed you did; and, as you know, your test does not suffice, since it
> does not always give the sign of the number 'i'.
>
> I repeat - you need to use the sign of the number 'i'.

Since the value of `i' is the return value of `parseInt(s, base)' here, in
the border case that `s' represents a number which absolute value is less
than 1, `i' has no sign or IOW its sign is always positive. It is therefore
hard to see how its sign could be useful.

> You so frequently insist that others search the archives; you should do
> so yourself. The matter was referred to sometime in the years 200x.
> Careful reading of ECMA 262 3/5 could help you.

Instead of making yet another lame attempt at an ad hominem attack, and
speaking in riddles, you could just have said how you would do it. Suppose,
just suppose, that I do exactly what you just did, then you are no better
than me. This should give you pause.


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
On Jun 2, 9:36 pm, Dr J R Stockton <reply1...(a)merlyn.demon.co.uk>
wrote:
> (...)
> You so frequently insist that others search the archives; you should do
> so yourself.  The matter was referred to sometime in the years 200x. (....)

Thanks for such a valuable pointer: we'll dig into 10 years of
threads.
--
Jorge.
From: Ry Nohryb on
On Jun 2, 4:08 pm, Scott Sauyet <scott.sau...(a)gmail.com> wrote:
> (...)
> It's an interesting result, but I'm not sure how much that really
> tells us about accuracy. (...)

How about this one ?

var exp= -1;
var inc= 1e-1;
var i= inc;

while ( inc && (i === parseFloat(i.toString(base), base)) ) {
var iSave= i;
i+= (inc= Math.pow(10, --exp));
}

--
Jorge.
From: Scott Sauyet on
Thomas 'PointedEars' Lahn wrote:
> Scott Sauyet wrote:
>> Thomas 'PointedEars' Lahn wrote:
>>> Scott Sauyet wrote:
>>>> Ry Nohryb wrote:
>>>>> Scott Sauyet wrote:
>>>>>> [ ... ]
>> (I should have included this line:)
>> |    var allDigits = "0123456789abcdefghijklmnopqrstuvwxyz",
>>>>>> parseFraction = function(str, base) {
>>>>>>   if (!str) return 0;
>>>>>>   var digits = str.split(""), total = 0;
>>>>>>   for (var i = digits.length; i--;) {
>>>>>>     total += allDigits.indexOf(digits[i]);
>>>>>>     total /= base;
>>>>>>   }
>>>>>>   return total;
>>>>>> };
>>>>>> [ ... ]
>>>>> I like that parseFraction() of yours, it's awesome. Good idea.
>>>> Thanks.  I'm not sure if it has any practical advantages, but it's at
>>>> least fairly clear mathematically.
>
>>> It is clear(er), but it is unfortunately not mathematically sound because
>>> we are dealing with *floating-point* arithmetics here, where precision is
>>> limited [ ... ]
>>> [ example elided ]
>
>> Yes, I understand that we are dealing with an implementation of
>> IEEE-754.  But I don't see how that makes my algorithm mathematically
>> unsound.  It is certainly not as efficient as the one you used, but I
>> think it might avoid some rounding issues.
>
> You are doing *more* floating-point operations, how can you have *less*
> rounding issues?

By being more precise. Your algorithm seems to throw away more
information than mine. At least a few random tests seem to indicate
so.


>> For instance, if parseFloat1 is the function you posted earlier [1]
>> and parseFloat2 is the one I posted [2], then in JavaScript 1.8.2
>
>>     parseFloat1("0.r3j6f0mqo4fr3j6f0m", 36).toString(36)
>
>> yields "0.r3j6f0mqo3m", whereas
>
>>     parseFloat2("0.r3j6f0mqo4fr3j6f0m", 36).toString(36))
>
>> yields "0.r3j6f0mqo4f", clearly a better approximation.
>
> No doubt about that, although I think you have the test case backwards.  

No, I've posted a test script at

<http://scott.sauyet.com/Javascript/Test/2010-06-03a/>

This case is

<http://scott.sauyet.com/Javascript/Test/2010-06-03a/?
base=36&value=0.r3j6f0mqo4fr3j6f0m>


> I have since tested my approach more thoroughly and accepted that {1,198} is
> flawed as it sacrifices too much precision.

If you have a new version, I can post a new version of that test page
with it, if you like, or you can modify it as you like. The PHP is
at:

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



> You need to compare the return value of yours against one of Jorge's that
> works (that in
> <99e57eb6-f5c7-410e-a40f-07ae48af2...(a)b21g2000vbh.googlegroups.com> returns
> NaN with your test case in JavaScript 1.8.2) instead.

Jorge's seems to generally agree with yours. Mine will sometimes end
up a closer match to the original string.

--
Scott