Horner's rule for polynomial evaluation: Difference between revisions

m
→‎version 1: added/changed comments, indentations, and whitespace.
No edit summary
m (→‎version 1: added/changed comments, indentations, and whitespace.)
Line 1,451:
=={{header|REXX}}==
===version 1===
<lang rexx>/*REXX program shows demonstrates using Horner's rule for polynomial evaluation. */
numeric digits 30 /*use extra numeric precision. */
parse arg x poly /*get value of X and the coefficients. */
equ$= /*start with equationa clean slate equation. */
do deg=0 until poly=='' /*get the equation's coefficients. /*works for any degree equation. */
do deg=0 until parse var poly=='' c.deg poly /*get the a equation's coefficients coefficient. */
parse var poly c.deg=c.deg poly/ 1 /*getnormalize it a(by dividing by 1). equation coefficient.*/
c.deg = c.deg / 1 if c.deg>=0 then c.deg= '+'c.deg /*if ¬ negative, then prefix /*normalize itwith (bya dividing by+ 1)*/
if c.deg>=0 then c.deg $=$ '+'c.deg /*ifconcatenate ¬it to the equation. neg, then prefix a + sign.*/
if deg\==0 & c.deg\=0 then $=$'∙x^'deg /*¬1st coefficient & ¬0? Append X pow.*/
equ=equ c.deg /*concatenate it to the equation.*/
$=$ ' ' /*insert some blanks, make it look nice*/
if deg\==0 & c.deg\=0 then equ= , /*if not the first coefficient & */
end /*jdeg*/
equ'∙x^'deg /* not 0, append power (^) of X.*/
equ = equsay ' ' x = ' /*insert some blanks, look pretty*/x
say ' end /*degree = ' deg*/
say ' equation = ' equ $
 
say ' x = ' x
say ' degree = ' deg
say ' equation = ' equ
a=c.deg
do j=deg by -1 for deg /*apply Horner's rule to the equations.*/
_ = j-1; a= a*x + c._
end a = a/*x + c._j*/
end /*j*/
say
say ' answer = ' a /*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when the following is used for input: &nbsp; <tt> 3 &nbsp; -19 &nbsp; 7 &nbsp; -4 &nbsp; 6 </tt>
<pre>
x = 3
Line 1,484 ⟶ 1,480:
answer = 128
</pre>
 
===version 2===
<lang rexx>/* REXX ---------------------------------------------------------------