Roots of a quadratic function: Difference between revisions

m
→‎version 1: simplified expressions in the subroutine and also the function, added an output case to show different formatting for another REXX.
m (→‎version 1: simplified expressions in the subroutine and also the function, added an output case to show different formatting for another REXX.)
Line 1,750:
 
Since "unlimited" decimal precision is part of the REXX language, the   '''numeric digits'''   was increased
<br>(from a default of &nbsp; '''9''') &nbsp; to &nbsp; '''200''' &nbsp; to accommodate when a root is closer to zero than the other root.
 
Note that only ten decimal digits (precision) are shown in the &nbsp; ''displaying'' &nbsp; of the output.
 
This REXX version supports &nbsp; ''complex numbers'' &nbsp; for the result.
Line 1,772:
quadratic: parse arg aa,bb,cc /*obtain the specified three arguments.*/
$=sqrt(bb**2-4*aa*cc); L=length($) /*compute SQRT (which may be complex).*/
rpr=1/(aa+aa); c ?=right($,1)=='i' /*compute reciprocal of 2*aa; Complex?*/
if c? then do; n=left($,L-1); r1= -bb*rp; *r; r2=r1; r1j=nleft($,L-1)*rpr; r2j=-r1j; end
else do; r1=(-bb+$)*rpr; r2=(-bb-$)*rpr; r1j=0; r1j=0; r2j= 0; end
return
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x 1 ox; if x=0 then return 0; d=digits(); i=; m.=9
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-abs(x; i='i'; end)
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g *.5'e'_ %2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
numeric digits d; return (g/1)left('i ',ox<0) /*make complex if X OX< 0.*/</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e5 &nbsp; 1 </tt>
<pre>
Line 1,792:
root2 = 0.000001
</pre>
The following output is when Regina 3.9.1 REXX is used.
 
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e9 &nbsp; 1 </tt>
<pre>
Line 1,800 ⟶ 1,802:
root1 = 1.000000000E+10
root2 = 1E-10
</pre>
The following output is when R4 REXX is used.
 
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e9 &nbsp; 1 </tt>
<pre>
a = 1
b = -1E+10
c = 1
 
root1 = 1E+10
root2 = 0.0000000001
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 3 &nbsp; 2 &nbsp; 1 </tt>