Anonymous user
Square root by hand: Difference between revisions
→{{header|REXX}}: cleaned up code so that if the square root is found, it stops spitting out zeroes.
m (→{{header|REXX}}: changed a comment, increased the width of the terminal.) |
(→{{header|REXX}}: cleaned up code so that if the square root is found, it stops spitting out zeroes.) |
||
Line 289:
parse arg xx digs . /*obtain optional arguments from the CL*/
if xx=='' | xx=="," then xx= 2 /*Not specified? Then use the default.*/
if digs=='' | digs=="," then digs=
numeric digits digs + digs % 2 /*ensure enough decimal digits for calc*/
call sqrtHand xx, digs /*invoke the function for sqrt by hand.*/
Line 299:
return r /*R is the integer square root of Z. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrtHand: parse arg x 1 ox,##; parse value iSqrt(x) with j 1 k 1 ? /*j, k, ? ≡ iSqrt(x)*/
if ?==0 then ?= /*handle the case of sqrt < 1. */
if j*j=x then do; say j; return; end /*have we found the exact sqrt?*/
L= length(?) /*L: used to place dec. point.*/
if L==0 then call spit .
if L>0 then do; call
if
if ?=='' then ?= 0 /*ensure ? is a valid digit.*/
x= (x - k*?) * 100; ?= 1
k= j * 20
Line 316 ⟶ 319:
j= ? + j*10
k= ? + k
end /*
return</lang>
{{out|output|text= when using the default inputs:}}
Line 331 ⟶ 334:
{{out|output|text= when using the inputs of: <tt> 10.89 80 </tt>}}
<pre>
3.3
</pre>
{{out|output|text= when using the inputs of: <tt> 625 </tt>}}
|