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

[email protected] (Solomon Foster) Wed, 7 Mar 2018 15:16:45 -0500
Newsgroups perl.perl6.language
Message-ID <CALpVjkgGSUEta1oxwGDsA3ROmK8+ZhO=RQRMg52Hw3eptnfbbg@mail.gmail.com>
--f403043619940a06b90566d83d97
Content-Type: text/plain; charset="UTF-8"

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

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

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On S=
un, Mar 4, 2018 at 8:49 AM, yary <span dir=3D"ltr">&lt;<a href=3D"mailto:no=
[email protected]" target=3D"_blank">[email protected]</a>&gt;</span> wrote:<=
br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;bord=
er-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 be set so these return=
 True:<br></div></div><br>&gt; pi =3D=3D pi.Rat # Does Num to Rat conversio=
n keep its precision?<br>False<br>&gt; pi.Str.Num =3D=3D pi # Does Num surv=
ive string round-trip? - Nothing to do with epsilon<br>False<br><br></div><=
/div></blockquote><div><br></div><div>Why on earth would you want to do thi=
s?</div><div><br></div><div>I mean that quite literally.=C2=A0 The only rea=
son I can see for directly comparing a Num and a Rat for equality is to che=
ck and see if the Rat has the same precision as the Num.=C2=A0 In practice,=
 it&#39;s well-known you generally shouldn&#39;t use equality tests on floa=
ting 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 som=
e quick notes.</div><div><br></div><div>1) You can pass 0 as the epsilon fo=
r the Rat constructor, which seems to be equivalent to very very small valu=
es 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 sti=
ll a Rat.)</div><div><br></div><div>3) Remember (I had forgotten!) that Num=
s can represent numbers much smaller than a Rat can.=C2=A0 1e-100 is a perf=
ectly reasonable Num, but (were Rat behaving properly) the closest possible=
 Rat value is 0.</div><div><br></div><div>4) That said, if you actually do=
=C2=A0(1e-100).Rat(0), it gives you=C2=A0(1 1000000000000000015902891109759=
9180468360808563945281389781327557747838772170381060813469985856815104).=C2=
=A0 Needless to say, that&#39;s not actually a legal Rat.=C2=A0 Surprisingl=
y (to me, anyway) it is accurate to better than 1e-110.</div><div><br></div=
><div>5) Somewhat more distressingly, (1e+100).Rat gives you (1000000000000=
000015902891109759918046836080856394528138978132755774783877217038106081346=
9985856815104 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 is a legal Rat.)</div><div><br></div><div>I admit don&#39;t real=
ly know what to do with this.</div></div><div><br></div>-- <br><div class=
=3D"gmail_signature">Solomon Foster: <a href=3D"mailto:[email protected]" t=
arget=3D"_blank">[email protected]</a><br>HarmonyWare, Inc: <a href=3D"http=
://www.harmonyware.com" target=3D"_blank">http://www.harmonyware.com</a></d=
iv>
</div></div>

--f403043619940a06b90566d83d97--