let me see what bug i left XD i tested it only for numbers above 1
and it got to full precision in few steps
FIXED:
it wasnt converging for numbers below 1 because the initial guess add opposite tendence to the square root.
now i'm pretty confident it works for every number
and that it has not got any corner cases
and it got to full precision in few steps
FIXED:
[SELECT ALL] Code:
// absval, square, and sqrt -- calculate absolute value, square, and square root -- by V-Man
alias absval [if (<f 0 $arg1) [result $arg1] [result (*f $arg1 -1)]]
// absval (value to calculate absolute value )
alias square [result (*f $arg1 $arg1)]
alias sqrt [
alias x (divf (+f $arg1 1) 2)
alias sqrt_prec 1e-05
tmp_iters = 0
sqrt_perr = (absval (-f (square $arg1) $arg1))
sqrt_err = (absval (-f (square $x) $arg1))
while [(|| (<f $sqrt_err $sqrt_perr) (< $tmp_iters 5))] [
-=f x (divf (-f (square $x) $arg1) (*f 2 $x))
sqrt_perr = $sqrt_err
sqrt_err = (absval (-f (square $x) $arg1))
+= tmp_iters 1
]
result $x
]
it wasnt converging for numbers below 1 because the initial guess add opposite tendence to the square root.
now i'm pretty confident it works for every number
and that it has not got any corner cases