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
|
# Finding sqrt(a) is the same as finding the roots of
|
||||||
# f(x) = x^2 - a
|
# 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)
|
||||||
|
lesser $in 0 &store
|
||||||
|
if $store %error
|
||||||
|
|
||||||
set &ans 2
|
set &ans 2
|
||||||
@loop
|
@loop
|
||||||
multiply $ans $ans &num
|
multiply $ans $ans &num
|
||||||
@@ -193,9 +196,7 @@ fun -double !sqrt -double &in
|
|||||||
multiply $ans 2 &dom
|
multiply $ans 2 &dom
|
||||||
|
|
||||||
divide $num $dom &store
|
divide $num $dom &store
|
||||||
lesser $store 0.00000001 &store2
|
equal $store 0 &store2
|
||||||
greater $store -0.00000001 &store3
|
|
||||||
equal $store2 $store3 &store2
|
|
||||||
if $store2 %end
|
if $store2 %end
|
||||||
|
|
||||||
subtract $ans $store &ans
|
subtract $ans $store &ans
|
||||||
@@ -203,8 +204,6 @@ fun -double !sqrt -double &in
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
return $ans
|
return $ans
|
||||||
endfun
|
|
||||||
|
|
||||||
pusharg 2.25
|
@error
|
||||||
call !sqrt &store
|
endfun
|
||||||
stdlnout $store
|
|
Reference in New Issue
Block a user