Roots of a quadratic function: Difference between revisions
Content added Content deleted
(Added Wren) |
m (→version 1: used nine digits precision for the output, simplified the code, use a template for the output sections, added/changed whitespace and comments.) |
||
Line 1,877: | Line 1,877: | ||
<br>(from a default of '''9''') to '''200''' to accommodate when a root is 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 |
Note that only nine 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. |
||
<lang rexx></lang> |
|||
<lang rexx>/*REXX program finds the roots (which may be complex) of a quadratic function.*/ |
|||
⚫ | |||
numeric digits 200 /*use enough digits to handle extremes.*/ |
|||
parse arg a b c . /*obtain the specified arguments: A B C*/ |
|||
call quadratic a,b,c /*solve quadratic function via the sub.*/ |
|||
numeric digits 10 /*reduce (output) digits for human eyes*/ |
|||
r1=r1/1; r2=r2/1; a=a/1; b=b/1; c=c/1 /*normalize numbers to the new digits. */ |
|||
if r1j\=0 then r1=r1 || left('+',r1j>0)(r1j/1)"i" /*handle complex number.*/ |
|||
if r2j\=0 then r2=r2 || left('+',r2j>0)(r2j/1)"i" /* " " " */ |
|||
say ' a =' a /*display the normalized value of A. */ |
|||
say ' b =' b /* " " " " " B. */ |
|||
say ' c =' c /* " " " " " C. */ |
|||
say; say 'root1 =' r1 /* " " " " 1st root*/ |
|||
say 'root2 =' r2 /* " " " " 2nd root*/ |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
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).*/ |
|||
r=1/(aa+aa); ?=right($,1)=='i' /*compute reciprocal of 2*aa; Complex?*/ |
|||
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 |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
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; x=abs(x) |
|||
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 OX<0.*/</lang> |
|||
⚫ | |||
<pre> |
<pre> |
||
a = 1 |
a = 1 |
||
Line 1,917: | Line 1,890: | ||
root2 = 0.000001 |
root2 = 0.000001 |
||
</pre> |
</pre> |
||
The following output is when Regina 3.9. |
The following output is when Regina 3.9.3 REXX is used. |
||
{{out|output|text= when using the input of: <tt> 1 -10e9 1 </tt>}} |
|||
<pre> |
<pre> |
||
a = 1 |
a = 1 |
||
b = - |
b = -1.0E+10 |
||
c = 1 |
c = 1 |
||
Line 1,930: | Line 1,903: | ||
The following output is when R4 REXX is used. |
The following output is when R4 REXX is used. |
||
{{out|output|text= when using the input of: <tt> 1 -10e9 1 </tt>}} |
|||
<pre> |
<pre> |
||
a = 1 |
a = 1 |
||
Line 1,939: | Line 1,912: | ||
root2 = 0.0000000001 |
root2 = 0.0000000001 |
||
</pre> |
</pre> |
||
{{out|output|text= when using the input of: <tt> 3 2 1 </tt>}} |
|||
<pre> |
<pre> |
||
a = 3 |
a = 3 |
||
Line 1,945: | Line 1,918: | ||
c = 1 |
c = 1 |
||
root1 = -0. |
root1 = -0.333333333+0.471404521i |
||
root2 = -0. |
root2 = -0.333333333-0.471404521i |
||
</pre> |
</pre> |
||
{{out|output|text= when using the input of: <tt> 1 0 1 </tt> |
|||
<pre> |
<pre> |
||
a = 1 |
a = 1 |