Jump to content

Roots of a quadratic function: Difference between revisions

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.
(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:
<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 tennine decimal digits (precision) are shown in the &nbsp; ''displaying'' &nbsp; of the output.
 
This REXX version supports &nbsp; ''complex numbers'' &nbsp; for the result.
<lang rexx></lang>
<lang rexx>/*REXX program finds the roots (which may be complex) of a quadratic function.*/
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; -10e5 &nbsp; 1 </tt>}}
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>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 1 &nbsp; -10e5 &nbsp; 1 </tt>
<pre>
a = 1
Line 1,917 ⟶ 1,890:
root2 = 0.000001
</pre>
The following output is when Regina 3.9.13 REXX is used.
 
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; -10e9 &nbsp; 1 </tt>}}
<pre>
a = 1
b = -100000000001.0E+10
c = 1
 
Line 1,930 ⟶ 1,903:
The following output is when R4 REXX is used.
 
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; -10e9 &nbsp; 1 </tt>}}
<pre>
a = 1
Line 1,939 ⟶ 1,912:
root2 = 0.0000000001
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 3 &nbsp; 2 &nbsp; 1 </tt>}}
<pre>
a = 3
Line 1,945 ⟶ 1,918:
c = 1
 
root1 = -0.3333333333333333333+0.4714045208i471404521i
root2 = -0.3333333333333333333-0.4714045208i471404521i
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 1 &nbsp; 0 &nbsp; 1 </tt>
<pre>
a = 1
Cookies help us deliver our services. By using our services, you agree to our use of cookies.