On Thu, 09 Jul 2020, Aureliano Guedes wrote:
> Hi all,
> 
> A quick question.
> 
> This is expected?
> 
> raku -e 'say 9 ** (1/3)'
> 2.080083823051904
> 
> 
> Why do I'm asking this?
> I know about the computational floating problem and I also know that the
> Raku deal with rational whenever it is possible and store numbers as
> expressions (actually I don't know if there are more details). That's why
> 0.2 + 0.1 == 0.3 in Raku returns True whilst in other languages like
> Python, Perl5, and Ruby return False.
> 
> Then, I was just playing around and I'd like to check if it is expected.
> 

I for one expected it. The equation 0.1 + 0.2 == 0.3 uses the Rat type in
Raku, which stores rational numbers as exact fractions of numerator and
denominator, avoiding the floating point inaccuracies you mention, and
hence making equality comparisons trustworthy.

The number 9 ** (1/3) is not rational and in general exponentiation with a
fractional power might not produce a rational number, so exact representation
via Rat is out of the question. Therefore Rakudo does the computation with
Num, which are inexact floating point values again.

Now, there are effective ways to represent algebraic numbers like 9 ** (1/3)
in such a way that you can do arithmetic with them, but I'm not aware of any
implementation of that available to Raku. For someone with enough tuits,
I think this [1] can serve as a sort of high-level manual for creating such
a module. In terms of ready-made non-Raku packages, I only know of CGAL [2],
which provides a clean and even (template-)parametric interface to real
algebraic numbers. In fact, I might have a try at NativeCall'ing it this
evening.

Best,
Tobias

[1] https://math.stackexchange.com/a/3144516
[2] https://www.cgal.org/

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk

Reply via email to