Roots of a quadratic function: Difference between revisions

Content added Content deleted
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: Line 1,750:


Since "unlimited" decimal precision is part of the REXX language, the   '''numeric digits'''   was increased
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 a root closer to zero than the other root.
<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 displaying of the output.
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.
This REXX version supports &nbsp; ''complex numbers'' &nbsp; for the result.
Line 1,772: Line 1,772:
quadratic: parse arg aa,bb,cc /*obtain the specified three arguments.*/
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).*/
$=sqrt(bb**2-4*aa*cc); L=length($) /*compute SQRT (which may be complex).*/
rp=1/(aa+aa); c?=right($,1)=='i' /*compute reciprocal of 2*aa; Complex?*/
r=1/(aa+aa); ?=right($,1)=='i' /*compute reciprocal of 2*aa; Complex?*/
if c? then do; n=left($,L-1); r1=-bb*rp; r2=r1; r1j=n*rp; r2j=-r1j; end
if ? then do; r1= -bb *r; r2=r1; r1j=left($,L-1)*r; r2j=-r1j; end
else do; r1=(-bb+$)*rp; r2=(-bb-$)*rp; r1j=0; r2j=0; end
else do; r1=(-bb+$)*r; r2=(-bb-$)*r; r1j=0; r2j= 0; end
return
return
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); i=; m.=9
sqrt: procedure; parse arg x 1 ox; if x=0 then return 0; d=digits(); m.=9
numeric digits 9; numeric form; h=d+6; if x<0 then do; x=-x; i='i'; end
numeric digits 9; numeric form; h=d+6; x=abs(x)
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; g=g*.5'e'_%2
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 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*/
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)i /*make complex if X < 0.*/</lang>
numeric digits d; return (g/1)left('i',ox<0) /*make complex if OX<0.*/</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e5 &nbsp; 1 </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e5 &nbsp; 1 </tt>
<pre>
<pre>
Line 1,792: Line 1,792:
root2 = 0.000001
root2 = 0.000001
</pre>
</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>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e9 &nbsp; 1 </tt>
<pre>
<pre>
Line 1,800: Line 1,802:
root1 = 1.000000000E+10
root1 = 1.000000000E+10
root2 = 1E-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>
</pre>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 3 &nbsp; 2 &nbsp; 1 </tt>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 3 &nbsp; 2 &nbsp; 1 </tt>