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 '''9''') to '''200''' to accommodate a root closer to zero than the other root. |
<br>(from a default of '''9''') to '''200''' 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 ''displaying'' of the output. |
||
This REXX version supports ''complex numbers'' for the result. |
This REXX version supports ''complex numbers'' 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).*/ |
||
r=1/(aa+aa); ?=right($,1)=='i' /*compute reciprocal of 2*aa; Complex?*/ |
|||
if |
if ? then do; r1= -bb *r; r2=r1; r1j=left($,L-1)*r; r2j=-r1j; 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() |
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; |
numeric digits 9; numeric form; h=d+6; x=abs(x) |
||
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; |
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; |
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; |
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/ |
||
numeric digits d; |
numeric digits d; return (g/1)left('i',ox<0) /*make complex if OX<0.*/</lang> |
||
'''output''' when using the input of: <tt> 1 -10e5 1 </tt> |
'''output''' when using the input of: <tt> 1 -10e5 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''' when using the input of: <tt> 1 -10e9 1 </tt> |
'''output''' when using the input of: <tt> 1 -10e9 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''' when using the input of: <tt> 1 -10e9 1 </tt> |
|||
<pre> |
|||
a = 1 |
|||
b = -1E+10 |
|||
c = 1 |
|||
root1 = 1E+10 |
|||
root2 = 0.0000000001 |
|||
</pre> |
</pre> |
||
'''output''' when using the input of: <tt> 3 2 1 </tt> |
'''output''' when using the input of: <tt> 3 2 1 </tt> |