why so different

--000000000000450b3f05a8b23647
Content-Type: text/plain; charset="UTF-8"

Hi all,

First, I'm naive in Raku. Then let's go to my question.

I'm trying to figure out why we got so different results here:

> e.Rat()**(pi.Rat()*i)
-0.9999999999999902-1.3942922582021257e-07i
> e**(pi*i)
-1+1.2246467991473532e-16i
> e.Rat()**(pi.Rat()*i) == e**(pi*i)
False

I understand the Num actually have the traditional behavior which leads
this:
0.1.Num() + 0.2.Num() != 0.3.Num()

And Rat is awesome cos deal nice to the real world.

Anyway, I do not expect so different results between Rat and Num.

Thanks,
Aureliano Guedes


-- 
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110

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

<div dir=3D"ltr">Hi all,<div><br></div><div>First, I&#39;m naive in Raku. T=
hen let&#39;s go to my question.=C2=A0<br><div><br></div><div>I&#39;m tryin=
g to figure out why we got so different results here:</div><div><br></div><=
div>&gt; e.Rat()**(pi.Rat()*i) =C2=A0 =C2=A0<br>-0.9999999999999902-1.39429=
22582021257e-07i<br>&gt; e**(pi*i)<br>-1+1.2246467991473532e-16i<br>&gt; e.=
Rat()**(pi.Rat()*i) =3D=3D e**(pi*i)<br>False<br><div><br></div><div>I unde=
rstand the Num actually have the traditional behavior which leads this:</di=
v><div>0.1.Num() + 0.2.Num() !=3D 0.3.Num()</div><div><br></div><div>And Ra=
t is awesome=C2=A0cos deal nice to the real world.=C2=A0</div><div><br></di=
v><div>Anyway, I do not expect so different results between=C2=A0Rat and Nu=
m.</div><div><br></div><div>Thanks,</div><div>Aureliano Guedes</div><div><b=
r></div><div><br></div>-- <br><div dir=3D"ltr" class=3D"gmail_signature" da=
ta-smartmail=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><di=
v><div dir=3D"ltr"><div><div dir=3D"ltr"><div style=3D"text-align:center">A=
ureliano Guedes</div><div style=3D"text-align:center"></div><div style=3D"t=
ext-align:center">skype: aureliano.guedes</div><div style=3D"text-align:cen=
ter">contato: =C2=A0(11) 94292-6110</div><div style=3D"text-align:center">w=
hatsapp +5511942926110</div></div></div></div></div></div></div></div></div=
></div></div></div>

--000000000000450b3f05a8b23647--
0
guedes
6/22/2020 8:37:20 PM
perl.perl6.users 1545 articles. 0 followers. Follow

4 Replies
1897 Views

Similar Articles

[PageSpeed] 52

On Mon, 22 Jun 2020, Aureliano Guedes wrote:
> Hi all,
> 
> First, I'm naive in Raku. Then let's go to my question.
> 
> I'm trying to figure out why we got so different results here:
> 
> > e.Rat()**(pi.Rat()*i)
> -0.9999999999999902-1.3942922582021257e-07i
> > e**(pi*i)
> -1+1.2246467991473532e-16i
> > e.Rat()**(pi.Rat()*i) == e**(pi*i)
> False
> 
> I understand the Num actually have the traditional behavior which leads
> this:
> 0.1.Num() + 0.2.Num() != 0.3.Num()
> 
> And Rat is awesome cos deal nice to the real world.
> 
> Anyway, I do not expect so different results between Rat and Num.
> 

First, e and π are not rational numbers, so Num (IEEE 754 double)
and Rat (pair of bigint numerator and denominator) both will be only
approximations.

The literals e and pi in Raku are typed as Num and when you call the
Rat method on them, you get a Rat back with the *default* precision
of 1e-6 [1]. That is a lot less precise than the Nums you started
with, hence the difference. Try supplying a large enough precision:

  > e.Rat(1e-200)**(pi.Rat(1e-200)*i)
  -1+1.2246467991473532e-16i

This is better than a default .Rat() but notice that you cannot get
past the limited precision in the source Nums, which is ca. 1e-16.

Best,
Tobias

[1] https://docs.raku.org/routine/Rat

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
0
tobs
6/22/2020 8:57:34 PM
--00000000000022ce2b05a8b2ac63
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thank you for the clarification.

There is a method to set Rat precision at the scope of the program to apply
to all *.Rat() cases?



On Mon, Jun 22, 2020 at 5:57 PM Tobias Boege <tobs@taboege.de> wrote:

> On Mon, 22 Jun 2020, Aureliano Guedes wrote:
> > Hi all,
> >
> > First, I'm naive in Raku. Then let's go to my question.
> >
> > I'm trying to figure out why we got so different results here:
> >
> > > e.Rat()**(pi.Rat()*i)
> > -0.9999999999999902-1.3942922582021257e-07i
> > > e**(pi*i)
> > -1+1.2246467991473532e-16i
> > > e.Rat()**(pi.Rat()*i) =3D=3D e**(pi*i)
> > False
> >
> > I understand the Num actually have the traditional behavior which leads
> > this:
> > 0.1.Num() + 0.2.Num() !=3D 0.3.Num()
> >
> > And Rat is awesome cos deal nice to the real world.
> >
> > Anyway, I do not expect so different results between Rat and Num.
> >
>
> First, e and =CF=80 are not rational numbers, so Num (IEEE 754 double)
> and Rat (pair of bigint numerator and denominator) both will be only
> approximations.
>
> The literals e and pi in Raku are typed as Num and when you call the
> Rat method on them, you get a Rat back with the *default* precision
> of 1e-6 [1]. That is a lot less precise than the Nums you started
> with, hence the difference. Try supplying a large enough precision:
>
>   > e.Rat(1e-200)**(pi.Rat(1e-200)*i)
>   -1+1.2246467991473532e-16i
>
> This is better than a default .Rat() but notice that you cannot get
> past the limited precision in the source Nums, which is ca. 1e-16.
>
> Best,
> Tobias
>
> [1] https://docs.raku.org/routine/Rat
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>


--=20
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110

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

<div dir=3D"ltr">Thank you for the clarification.=C2=A0<br><div><br></div><=
div>There is a method to set Rat precision at the scope of the program to a=
pply to all *.Rat() cases?</div><div><br></div><div><br></div></div><br><di=
v class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr">On Mon, Jun 2=
2, 2020 at 5:57 PM Tobias Boege &lt;<a href=3D"mailto:tobs@taboege.de">tobs=
@taboege.de</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding=
-left:1ex">On Mon, 22 Jun 2020, Aureliano Guedes wrote:<br>
&gt; Hi all,<br>
&gt; <br>
&gt; First, I&#39;m naive in Raku. Then let&#39;s go to my question.<br>
&gt; <br>
&gt; I&#39;m trying to figure out why we got so different results here:<br>
&gt; <br>
&gt; &gt; e.Rat()**(pi.Rat()*i)<br>
&gt; -0.9999999999999902-1.3942922582021257e-07i<br>
&gt; &gt; e**(pi*i)<br>
&gt; -1+1.2246467991473532e-16i<br>
&gt; &gt; e.Rat()**(pi.Rat()*i) =3D=3D e**(pi*i)<br>
&gt; False<br>
&gt; <br>
&gt; I understand the Num actually have the traditional behavior which lead=
s<br>
&gt; this:<br>
&gt; 0.1.Num() + 0.2.Num() !=3D 0.3.Num()<br>
&gt; <br>
&gt; And Rat is awesome cos deal nice to the real world.<br>
&gt; <br>
&gt; Anyway, I do not expect so different results between Rat and Num.<br>
&gt; <br>
<br>
First, e and =CF=80 are not rational numbers, so Num (IEEE 754 double)<br>
and Rat (pair of bigint numerator and denominator) both will be only<br>
approximations.<br>
<br>
The literals e and pi in Raku are typed as Num and when you call the<br>
Rat method on them, you get a Rat back with the *default* precision<br>
of 1e-6 [1]. That is a lot less precise than the Nums you started<br>
with, hence the difference. Try supplying a large enough precision:<br>
<br>
=C2=A0 &gt; e.Rat(1e-200)**(pi.Rat(1e-200)*i)<br>
=C2=A0 -1+1.2246467991473532e-16i<br>
<br>
This is better than a default .Rat() but notice that you cannot get<br>
past the limited precision in the source Nums, which is ca. 1e-16.<br>
<br>
Best,<br>
Tobias<br>
<br>
[1] <a href=3D"https://docs.raku.org/routine/Rat" rel=3D"noreferrer" target=
=3D"_blank">https://docs.raku.org/routine/Rat</a><br>
<br>
-- <br>
&quot;There&#39;s an old saying: Don&#39;t change anything... ever!&quot; -=
- Mr. Monk<br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div=
 dir=3D"ltr"><div><div dir=3D"ltr"><div style=3D"text-align:center">Aurelia=
no Guedes</div><div style=3D"text-align:center"></div><div style=3D"text-al=
ign:center">skype: aureliano.guedes</div><div style=3D"text-align:center">c=
ontato: =C2=A0(11) 94292-6110</div><div style=3D"text-align:center">whatsap=
p +5511942926110</div></div></div></div></div></div></div></div></div>

--00000000000022ce2b05a8b2ac63--
0
guedes
6/22/2020 9:10:17 PM
On Mon, 22 Jun 2020, Aureliano Guedes wrote:
> Thank you for the clarification.
> 
> There is a method to set Rat precision at the scope of the program to apply
> to all *.Rat() cases?
> 

I don't know, based on a quick search in the documentation, of any global
setting. The hacky solution would be to wrap the method like this:

  say pi.Rat.nude;  # OUTPUT: (355 113)

  Num.^find_method('Rat').wrap: -> |c {
      # Make the default argument 1e-20
      callwith |(|c, 1e-20 if c.elems ≤ 1)
  };

  say pi.Rat.nude;  # OUTPUT: (245850922 78256779)

This program has three parts. First, we print pi.Rat.nude for comparison.
The nude method returns numerator and denominator of a Rat as a list.
The last part is just looking at pi.Rat.nude again to confirm that the
change in the middle has worked and increased the default precision.

You said you are relatively new to Raku, so let me explain the middle part.
Raku has a special syntax for calling metamethods (i.e. doing introspection)
on an object or type, which is the .^ operator. I use it to access an object
representation of the Rat method on the Num type. I can wrap some code of
my liking around this method via its wrap method.

The `-> |c { … }` syntax creates an anonymous code block that wraps around
the Num.Rat method and it accepts all arguments that may be passed as a
Capture [1] in the variable c. If that Capture does not have a first
parameter, i.e. the precision is not specified, I sneak in my 1e-20.
Afterwards, the `callwith |(…)` part unpacks the (modified) captured
arguments and calls the original method that I am wrapping [2].

Effectively, I pass a precision of 1e-20 whenever the caller did not pass
a precision. You can see that the wrapping takes effect immediately in
the last line of the above program.

Ideally, this could be turned into a module that hides the questionable
means behind a dynamic $*RAT-PRECISION variable or so. That is, unless
someone comes up with an even better solution.

Best,
Tobias

[1] https://docs.raku.org/type/Capture
[2] https://docs.raku.org/routine/wrap

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk
0
tobs
6/22/2020 9:41:39 PM
--000000000000ef86f305a8b3d1fc
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Thanks again.

I'm trying to get proficiency in Raku by converting some Python programs
mine in Raku.

On Mon, Jun 22, 2020 at 6:41 PM Tobias Boege <tobs@taboege.de> wrote:

> On Mon, 22 Jun 2020, Aureliano Guedes wrote:
> > Thank you for the clarification.
> >
> > There is a method to set Rat precision at the scope of the program to
> apply
> > to all *.Rat() cases?
> >
>
> I don't know, based on a quick search in the documentation, of any global
> setting. The hacky solution would be to wrap the method like this:
>
>   say pi.Rat.nude;  # OUTPUT: (355 113)
>
>   Num.^find_method('Rat').wrap: -> |c {
>       # Make the default argument 1e-20
>       callwith |(|c, 1e-20 if c.elems =E2=89=A4 1)
>   };
>
>   say pi.Rat.nude;  # OUTPUT: (245850922 78256779)
>
> This program has three parts. First, we print pi.Rat.nude for comparison.
> The nude method returns numerator and denominator of a Rat as a list.
> The last part is just looking at pi.Rat.nude again to confirm that the
> change in the middle has worked and increased the default precision.
>
> You said you are relatively new to Raku, so let me explain the middle par=
t.
> Raku has a special syntax for calling metamethods (i.e. doing
> introspection)
> on an object or type, which is the .^ operator. I use it to access an
> object
> representation of the Rat method on the Num type. I can wrap some code of
> my liking around this method via its wrap method.
>
> The `-> |c { =E2=80=A6 }` syntax creates an anonymous code block that wra=
ps around
> the Num.Rat method and it accepts all arguments that may be passed as a
> Capture [1] in the variable c. If that Capture does not have a first
> parameter, i.e. the precision is not specified, I sneak in my 1e-20.
> Afterwards, the `callwith |(=E2=80=A6)` part unpacks the (modified) captu=
red
> arguments and calls the original method that I am wrapping [2].
>
> Effectively, I pass a precision of 1e-20 whenever the caller did not pass
> a precision. You can see that the wrapping takes effect immediately in
> the last line of the above program.
>
> Ideally, this could be turned into a module that hides the questionable
> means behind a dynamic $*RAT-PRECISION variable or so. That is, unless
> someone comes up with an even better solution.
>
> Best,
> Tobias
>
> [1] https://docs.raku.org/type/Capture
> [2] https://docs.raku.org/routine/wrap
>
> --
> "There's an old saying: Don't change anything... ever!" -- Mr. Monk
>


--=20
Aureliano Guedes
skype: aureliano.guedes
contato:  (11) 94292-6110
whatsapp +5511942926110

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

<div dir=3D"ltr">Thanks again.<div><br></div><div>I&#39;m trying to get pro=
ficiency=C2=A0in Raku by converting some Python programs mine in Raku.</div=
></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_attr"=
>On Mon, Jun 22, 2020 at 6:41 PM Tobias Boege &lt;<a href=3D"mailto:tobs@ta=
boege.de">tobs@taboege.de</a>&gt; wrote:<br></div><blockquote class=3D"gmai=
l_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,20=
4,204);padding-left:1ex">On Mon, 22 Jun 2020, Aureliano Guedes wrote:<br>
&gt; Thank you for the clarification.<br>
&gt; <br>
&gt; There is a method to set Rat precision at the scope of the program to =
apply<br>
&gt; to all *.Rat() cases?<br>
&gt; <br>
<br>
I don&#39;t know, based on a quick search in the documentation, of any glob=
al<br>
setting. The hacky solution would be to wrap the method like this:<br>
<br>
=C2=A0 say pi.Rat.nude;=C2=A0 # OUTPUT: (355 113)<br>
<br>
=C2=A0 Num.^find_method(&#39;Rat&#39;).wrap: -&gt; |c {<br>
=C2=A0 =C2=A0 =C2=A0 # Make the default argument 1e-20<br>
=C2=A0 =C2=A0 =C2=A0 callwith |(|c, 1e-20 if c.elems =E2=89=A4 1)<br>
=C2=A0 };<br>
<br>
=C2=A0 say pi.Rat.nude;=C2=A0 # OUTPUT: (245850922 78256779)<br>
<br>
This program has three parts. First, we print pi.Rat.nude for comparison.<b=
r>
The nude method returns numerator and denominator of a Rat as a list.<br>
The last part is just looking at pi.Rat.nude again to confirm that the<br>
change in the middle has worked and increased the default precision.<br>
<br>
You said you are relatively new to Raku, so let me explain the middle part.=
<br>
Raku has a special syntax for calling metamethods (i.e. doing introspection=
)<br>
on an object or type, which is the .^ operator. I use it to access an objec=
t<br>
representation of the Rat method on the Num type. I can wrap some code of<b=
r>
my liking around this method via its wrap method.<br>
<br>
The `-&gt; |c { =E2=80=A6 }` syntax creates an anonymous code block that wr=
aps around<br>
the Num.Rat method and it accepts all arguments that may be passed as a<br>
Capture [1] in the variable c. If that Capture does not have a first<br>
parameter, i.e. the precision is not specified, I sneak in my 1e-20.<br>
Afterwards, the `callwith |(=E2=80=A6)` part unpacks the (modified) capture=
d<br>
arguments and calls the original method that I am wrapping [2].<br>
<br>
Effectively, I pass a precision of 1e-20 whenever the caller did not pass<b=
r>
a precision. You can see that the wrapping takes effect immediately in<br>
the last line of the above program.<br>
<br>
Ideally, this could be turned into a module that hides the questionable<br>
means behind a dynamic $*RAT-PRECISION variable or so. That is, unless<br>
someone comes up with an even better solution.<br>
<br>
Best,<br>
Tobias<br>
<br>
[1] <a href=3D"https://docs.raku.org/type/Capture" rel=3D"noreferrer" targe=
t=3D"_blank">https://docs.raku.org/type/Capture</a><br>
[2] <a href=3D"https://docs.raku.org/routine/wrap" rel=3D"noreferrer" targe=
t=3D"_blank">https://docs.raku.org/routine/wrap</a><br>
<br>
-- <br>
&quot;There&#39;s an old saying: Don&#39;t change anything... ever!&quot; -=
- Mr. Monk<br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
 class=3D"gmail_signature"><div dir=3D"ltr"><div><div dir=3D"ltr"><div><div=
 dir=3D"ltr"><div><div dir=3D"ltr"><div style=3D"text-align:center">Aurelia=
no Guedes</div><div style=3D"text-align:center"></div><div style=3D"text-al=
ign:center">skype: aureliano.guedes</div><div style=3D"text-align:center">c=
ontato: =C2=A0(11) 94292-6110</div><div style=3D"text-align:center">whatsap=
p +5511942926110</div></div></div></div></div></div></div></div></div>

--000000000000ef86f305a8b3d1fc--
0
guedes
6/22/2020 10:32:26 PM
Reply: