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

Reply via email to