Fix sqrt function (again)
This commit is contained in:
@@ -185,6 +185,9 @@ 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)
|
||||
lesser $in 0 &store
|
||||
if $store %error
|
||||
|
||||
set &ans 2
|
||||
@loop
|
||||
multiply $ans $ans &num
|
||||
@@ -193,9 +196,7 @@ fun -double !sqrt -double &in
|
||||
multiply $ans 2 &dom
|
||||
|
||||
divide $num $dom &store
|
||||
lesser $store 0.00000001 &store2
|
||||
greater $store -0.00000001 &store3
|
||||
equal $store2 $store3 &store2
|
||||
equal $store 0 &store2
|
||||
if $store2 %end
|
||||
|
||||
subtract $ans $store &ans
|
||||
@@ -203,8 +204,6 @@ fun -double !sqrt -double &in
|
||||
|
||||
@end
|
||||
return $ans
|
||||
endfun
|
||||
|
||||
pusharg 2.25
|
||||
call !sqrt &store
|
||||
stdlnout $store
|
||||
@error
|
||||
endfun
|
Reference in New Issue
Block a user