Re: (default) Real->Rat precision should match what compiler uses for literals

[email protected] (yary) Wed, 14 Mar 2018 20:42:36 -0300
Newsgroups perl.perl6.language
Message-ID <CAG2CFAYtpectcwANCsGHUsUFU6zUeNngJbLvEQNKj7uRFYx+YQ@mail.gmail.com>
--001a113b4cea465234056767ef8d
Content-Type: text/plain; charset="UTF-8"

I want an epsilon that doesn't confuse newbies and which also is efficient.
epsilon=1/2**(mantissa bits-1) fits the bill.

Why I want this-  It would be great to have numbers survive round-trip
conversions, when feasible.

Specifically I have no need to compare Rats and Nums for equality, but I do
often deal with "flat" text files full of metrics, and remotely sourced
json, and XML. The data types are sometimes unexpected.

-y

On Wed, Mar 7, 2018 at 5:16 PM, Solomon Foster <[email protected]> wrote:

> On Sun, Mar 4, 2018 at 8:49 AM, yary <[email protected]> wrote:
>
>> In that spirit, I'd expect numeric comparison in general, and epsilon
>> specifically, to be set so these return True:
>>
>> > pi == pi.Rat # Does Num to Rat conversion keep its precision?
>> False
>> > pi.Str.Num == pi # Does Num survive string round-trip? - Nothing to do
>> with epsilon
>> False
>>
>>
> Why on earth would you want to do this?
>
> I mean that quite literally.  The only reason I can see for directly
> comparing a Num and a Rat for equality is to check and see if the Rat has
> the same precision as the Num.  In practice, it's well-known you generally
> shouldn't use equality tests on floating point numbers.  Converting one
> side of the equation to a Rat just makes it make even less sense.
>
>
> I've just been playing around with Num to Rat conversion, and here are
> some quick notes.
>
> 1) You can pass 0 as the epsilon for the Rat constructor, which seems to
> be equivalent to very very small values of epsilon.
>
> 2)  pi.Rat(0) + exp(1).Rat(0) is a Rat, but pi.Rat(0) + exp(1).Rat(0) +
> sin(.2).Rat(0) is a Num.  (On the other hand, pi.Rat() + exp(1).Rat() +
> sin(.2).Rat() is still a Rat.)
>
> 3) Remember (I had forgotten!) that Nums can represent numbers much
> smaller than a Rat can.  1e-100 is a perfectly reasonable Num, but (were
> Rat behaving properly) the closest possible Rat value is 0.
>
> 4) That said, if you actually do (1e-100).Rat(0), it gives you (1
> 100000000000000001590289110975991804683608085639452813897813
> 27557747838772170381060813469985856815104).  Needless to say, that's not
> actually a legal Rat.  Surprisingly (to me, anyway) it is accurate to
> better than 1e-110.
>
> 5) Somewhat more distressingly, (1e+100).Rat gives you (
> 100000000000000001590289110975991804683608085639452813897813
> 27557747838772170381060813469985856815104 1).  That's only accurate to
> 10**83.  Which is to say, it's as accurate as a double gets -- 16-17
> digits.   (BTW, that is a legal Rat.)
>
> I admit don't really know what to do with this.
>
> --
> Solomon Foster: [email protected]
> HarmonyWare, Inc: http://www.harmonyware.com
>

--001a113b4cea465234056767ef8d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>I want an epsilon that doesn&#39;t confuse newbies an=
d which also is efficient. epsilon=3D1/2**(mantissa bits-1) fits the bill.<=
br><br></div><div>Why I want this-=C2=A0 It would be great to have numbers =
survive round-trip conversions, when feasible.<br><br>Specifically I have n=
o need to compare Rats and Nums for equality, but I do often deal with &quo=
t;flat&quot; text files full of metrics, and remotely sourced json, and XML=
. The data types are sometimes unexpected.<br></div></div><div class=3D"gma=
il_extra"><br clear=3D"all"><div><div class=3D"gmail_signature" data-smartm=
ail=3D"gmail_signature">-y<br></div></div>
<br><div class=3D"gmail_quote">On Wed, Mar 7, 2018 at 5:16 PM, Solomon Fost=
er <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_bl=
ank">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_=
quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1=
ex"><div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">=
<span class=3D"">On Sun, Mar 4, 2018 at 8:49 AM, yary <span dir=3D"ltr">&lt=
;<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]</=
a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0=
px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><=
div dir=3D"ltr"><div><div><div style=3D"margin-left:40px">In that spirit, I=
&#39;d expect numeric comparison in general, and epsilon specifically, to b=
e set so these return True:<br></div></div><br>&gt; pi =3D=3D pi.Rat # Does=
 Num to Rat conversion keep its precision?<br>False<br>&gt; pi.Str.Num =3D=
=3D pi # Does Num survive string round-trip? - Nothing to do with epsilon<b=
r>False<br><br></div></div></blockquote><div><br></div></span><div>Why on e=
arth would you want to do this?</div><div><br></div><div>I mean that quite =
literally.=C2=A0 The only reason I can see for directly comparing a Num and=
 a Rat for equality is to check and see if the Rat has the same precision a=
s the Num.=C2=A0 In practice, it&#39;s well-known you generally shouldn&#39=
;t use equality tests on floating point numbers.=C2=A0 Converting one side =
of the equation to a Rat just makes it make even less sense.</div><div><br>=
</div><div><br></div><div>I&#39;ve just been playing around with Num to Rat=
 conversion, and here are some quick notes.</div><div><br></div><div>1) You=
 can pass 0 as the epsilon for the Rat constructor, which seems to be equiv=
alent to very very small values of epsilon.</div><div><br></div><div>2)=C2=
=A0 pi.Rat(0) + exp(1).Rat(0) is a Rat, but=C2=A0pi.Rat(0) + exp(1).Rat(0) =
+ sin(.2).Rat(0) is a Num.=C2=A0 (On the other hand,=C2=A0pi.Rat() + exp(1)=
.Rat() + sin(.2).Rat() is still a Rat.)</div><div><br></div><div>3) Remembe=
r (I had forgotten!) that Nums can represent numbers much smaller than a Ra=
t can.=C2=A0 1e-100 is a perfectly reasonable Num, but (were Rat behaving p=
roperly) the closest possible Rat value is 0.</div><div><br></div><div>4) T=
hat said, if you actually do=C2=A0(1e-100).Rat(0), it gives you=C2=A0(1 100=
000000000000001590289110975<wbr>991804683608085639452813897813<wbr>27557747=
8387721703810608134699<wbr>85856815104).=C2=A0 Needless to say, that&#39;s =
not actually a legal Rat.=C2=A0 Surprisingly (to me, anyway) it is accurate=
 to better than 1e-110.</div><div><br></div><div>5) Somewhat more distressi=
ngly, (1e+100).Rat gives you (<wbr>100000000000000001590289110975<wbr>99180=
4683608085639452813897813<wbr>275577478387721703810608134699<wbr>8585681510=
4 1).=C2=A0 That&#39;s only accurate to 10**83.=C2=A0 Which is to say, it&#=
39;s as accurate as a double gets -- 16-17 digits.=C2=A0 =C2=A0(BTW, that i=
s a legal Rat.)</div><div><br></div><div>I admit don&#39;t really know what=
 to do with this.</div></div><span class=3D""><div><br></div>-- <br><div cl=
ass=3D"m_5202827593304765931gmail_signature">Solomon Foster: <a href=3D"mai=
lto:[email protected]" target=3D"_blank">[email protected]</a><br>HarmonyWa=
re, Inc: <a href=3D"http://www.harmonyware.com" target=3D"_blank">http://ww=
w.harmonyware.com</a></div>
</span></div></div>
</blockquote></div><br></div>

--001a113b4cea465234056767ef8d--