Front page | perl.perl6.users | Postings from July 2020

## Re: root cubic

From:
William Michels via perl6-users
Date:
July 9, 2020 18:25
Subject:
Re: root cubic
Message ID:
CAA99HCzDRK=wJDbc=U-bqHB0+rcAv2QQvk3gPczWS4xuX4LcwA@mail.gmail.com
```> #in the Raku/Perl6 REPL
> say 4 ** (1/2)
2
> say 8 ** (1/3)
2
> 4 ** (1/2) == 8 ** (1/3)
True
> say 8 ** (1/4)
1.681792830507429
>
> say 9 ** (1/2)
3
> say 9 ** (1/3)
2.080083823051904
> say 9 ** (1/4)
1.7320508075688772
> \$*VM
moar (2020.06)
>

Is the question whether Raku can somehow understand/represent
square-roots and/or cube-roots?
E.g.  8**(1/2) = 2√2  ?
Otherwise, the results above seem reasonable.

HTH, Bill.

On Thu, Jul 9, 2020 at 10:28 AM Ralph Mellor <ralphdjmellor@gmail.com> wrote:
>
> You have several useful answers.
>
> I want to summarize what they saButy, and add a couple resources.
>
> * You probably meant 9 ** <1/2>.
>   Which will display 3.
>   But it's a Num. It's not the right type if you care about accuracy.
>
> * A solution to get rationals right is built into Raku.
>   See https://medium.com/@raiph_mellor/fixed-point-is-still-an-approximation-727c6facdbff
>   But it won't work for `**` because it coerces to Num if the RHS is rational.
>
> * The full solution to getting math right is symbolic math.
>   For Raku that means https://github.com/raydiak/Math-Symbolic
>   Note that it includes features for roots.
>   Note that we would all love to hear it works for you for
>       equivalents of 9 ** <1/2> and/or 27 ** <1/3>
>   In particular, not that the result *seems* right,
>       but that it is the right type. (Int or Rat).
>
> Hth.
>
> On Thu, Jul 9, 2020 at 5:11 PM Tobias Boege <tobs@taboege.de> wrote:
> >
> > 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

```