Fixed sqrt function

This commit is contained in:
2025-09-11 13:07:48 +10:00
parent 339fa0d7fa
commit 77e59af4bf

View File

@@ -184,15 +184,18 @@ endfun
fun -double !sqrt -double &in
# Finding sqrt(a) is the same as finding the roots of
# f(x) = x^2 - a
# Using Newton's method: x_(k+1) = x_k - f(x_k)/f'(x_k) = x_k - ((x_k)^2+a)/2(x_k)
# Using Newton's method: x_(k+1) = x_k - f(x_k)/f'(x_k) = x_k - ((x_k)^2-a)/2(x_k)
set &ans 2
@loop
multiply $ans $ans &num
add $ans $in &num
subtract $num $in &num
multiply $ans 2 &dom
divide $num $dom &store
equal $store 0 &store2
lesser $store 0.00000001 &store2
greater $store -0.00000001 &store3
equal $store2 $store3 &store2
if $store2 %end
subtract $ans $store &ans
@@ -201,3 +204,7 @@ fun -double !sqrt -double &in
@end
return $ans
endfun
pusharg 2.25
call !sqrt &store
stdlnout $store