develooper Front page | perl.perl6.users | Postings from November 2021

Re: how do I turn a real into and array of Integers?

Thread Previous | Thread Next
From:
ToddAndMargo via perl6-users
Date:
November 1, 2021 03:35
Subject:
Re: how do I turn a real into and array of Integers?
Message ID:
2b36075c-8232-3ee3-19b9-3c363c20f7e2@zoho.com
On 10/31/21 19:39, Sean McAfee wrote:
> On Sun, Oct 31, 2021 at 6:51 PM ToddAndMargo via perl6-users 
> <perl6-users@perl.org <mailto:perl6-users@perl.org>> wrote:
> 
>      >> How do I get more digits out of sqrt?
> 
>     On 10/31/21 17:50, Kevin Pye wrote:
>       > You don't.
>       >
>       > sqrt is a function which acts on 64-bit floating point numbers, and
>     there's no more meaningful digits available. If you need more precision
>     you're on your own.
> 
>     Dang!  I was hoping ther was some thing like UInt.
> 
> 
> Fortunately, it's easy.
> 
> Wikipedia describes the following recurrence formula for approximating 
> the square root of 2 from an initial guess a₀:
> 
>      aₙ₊₁ = (aₙ / 2 + 1 / aₙ)
> 
> So just iterate this sequence until successive terms become close enough 
> for you, using the unlimited precision FatRat type, for example:
> 
>      (2.FatRat, { $_ / 2 + 1 / $_ } ... (* - *).abs < 1e-100).tail
> 
> This gives the root to a precision of one in a googol.
> 

 > (2.FatRat, { $_ / 2 + 1 / $_ } ... (* - *).abs < 1e-100).tail
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571


Awesome!

Thread Previous | Thread Next


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