develooper Front page | perl.perl6.users | Postings from June 2022

Re: tons of digits

Thread Previous
From:
Bruce Gray
Date:
June 11, 2022 05:37
Subject:
Re: tons of digits
Message ID:
A8F5E658-7CD0-4F1D-98AD-67B9C8F2855F@acm.org


> On Jun 10, 2022, at 8:01 AM, ToddAndMargo via perl6-users <perl6-users@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)


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About