Square Root: Digit by Digit Algorithm
11-25-2022, 10:59 AM
Post: #5
 Thomas Klemm Senior Member Posts: 1,830 Joined: Dec 2013
RE: Square Root: Digit by Digit Algorithm
(11-25-2022 09:30 AM)brouhaha Wrote:  if there's a fixed-length field that specifies the length of the variable-length number, then there is an implementation limit, though it might be bigger than the amount of DRAM in existence.

As a playground I recommend to use an online Jupyter-Lab.
There mpmath is preinstalled.

Code:
from mpmath import *

The Naïve Way

Code:
mp.dps = 10 a = sqrt(2) b = log(a) a, b

(mpf('1.41421356237'), mpf('0.3465735902791'))

Code:
mp.dps = 100 a, b

(mpf('1.414213562369695864617824554443359375'),
mpf('0.3465735902791493572294712066650390625'))

First I was surprised but then I noticed: nah, that aren't 100 digits.

The Lazy Way

Using functions as numbers we can postpone the evaluation.

Code:
mp.dps = 10 a = lambda: sqrt(2) b = lambda: log(a()) a(), b()

(mpf('1.41421356237'), mpf('0.3465735902791'))

Code:
mp.dps = 100 a(), b()

(mpf('1.414213562373095048801688724209698078569671875376948073176679737990732478​462107038850387534327641572735'),
mpf('0.3465735902799726547086160607290882840377500671801276270603400047466968109​848473578029316634982093437698'))

The usage is rather clumsy but could be improved by wrapping these "lazy numbers" in a class.
Digits that have been calculated could be cached.