Fixed sqrt function
This commit is contained in:
@@ -184,15 +184,18 @@ endfun
|
|||||||
fun -double !sqrt -double &in
|
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)
|
||||||
|
set &ans 2
|
||||||
@loop
|
@loop
|
||||||
multiply $ans $ans &num
|
multiply $ans $ans &num
|
||||||
add $ans $in &num
|
subtract $num $in &num
|
||||||
|
|
||||||
multiply $ans 2 &dom
|
multiply $ans 2 &dom
|
||||||
|
|
||||||
divide $num $dom &store
|
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
|
if $store2 %end
|
||||||
|
|
||||||
subtract $ans $store &ans
|
subtract $ans $store &ans
|
||||||
@@ -200,4 +203,8 @@ fun -double !sqrt -double &in
|
|||||||
|
|
||||||
@end
|
@end
|
||||||
return $ans
|
return $ans
|
||||||
endfun
|
endfun
|
||||||
|
|
||||||
|
pusharg 2.25
|
||||||
|
call !sqrt &store
|
||||||
|
stdlnout $store
|
Reference in New Issue
Block a user