diff --git a/packages/math/math.grnd b/packages/math/math.grnd index 95dae23..45f609c 100644 --- a/packages/math/math.grnd +++ b/packages/math/math.grnd @@ -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 @@ -200,4 +203,8 @@ fun -double !sqrt -double &in @end return $ans -endfun \ No newline at end of file +endfun + +pusharg 2.25 +call !sqrt &store +stdlnout $store \ No newline at end of file