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

Re: tons of digits

Thread Previous
Bruce Gray
June 11, 2022 05:37
Re: tons of digits
Message ID:

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

Hope this helps,
Bruce Gray (Util of PerlMonks)

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About