> On Jun 10, 2022, at 8:01 AM, ToddAndMargo via perl6-users
> <perl6-us...@perl.org> wrote:
>
> Hi All,
> I forgot how to write out a ton of digits for the square root of 2 (or any
> other irrational number).
Hi Todd,
I suspect that this message is what you have misplaced:
https://www.nntp.perl.org/group/perl.perl6.users/2021/10/msg10221.html
Subject: Re: how do I turn a real into and array of Integers?
In that message, I pointed to an integer_root RosettaCode implementation that,
when used `2` scaled-up by 100²⁰⁰⁰, can easily produce 2000 digits.
Alternately, you can use Math::MPFR from Perl 5 to interface to the GNU MPFR
Library, which has many custom algorithms for crazy-precision irrationals
built-in.
raku -e '
sub precise_square_root ( Numeric $N, UInt $decimal_precision --> Str ) {
use Math::MPFR:from<Perl5> qw<:mpfr>;
my $binary_precision = ceiling( $decimal_precision * log2(10) );
my $bn = Rmpfr_init2($binary_precision);
Rmpfr_set_d($bn, $N , MPFR_RNDN);
Rmpfr_sqrt( $bn, $bn, MPFR_RNDN);
return Rmpfr_get_str($bn, 10, 0, MPFR_RNDN);
}
say precise_square_root(2, 400);
'
Output:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884709
--
Hope this helps,
Bruce Gray (Util of PerlMonks)