On 10/31/21 19:39, Sean McAfee wrote:
On Sun, Oct 31, 2021 at 6:51 PM ToddAndMargo via perl6-users
<perl6-us...@perl.org <mailto:perl6-us...@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!