/* * ============================================================== * Evaluation of f(x) = 2 + 3.x + 4.x^2 + 5.x^3 + 6.x^4 * using Horner's Rule = 2 + x.(3. + x.(4 + x.(5. + 6.x))) * * Copyright (C) 1998 by Mark Austin and David Chancogne. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By : Mark Austin * ============================================================== */ #include int main( void ) { float fA = 2.0F; float fB = 3.0F; float fC = 4.0F; float fD = 5.0F; float fE = 6.0F; float fX1; /* [a] : Prompt user for value of function variable */ printf("Please enter coefficient fX : "); fflush(stdout) scanf("%f%*c", &fX); /* [b] : Evaluate polynomial as sum of product terms */ fX1 = fA + fB*fX + fC*fX*fX + fD*fX*fX*fX + fE*fX*fX*fX*fX; printf("Sum of Product terms : F(%4.2f) = %4.2f\n", fX ,fX1 ); /* [c] : Evaluate polynomial using Horner's Rule */ fX1 = fA + fX*(fB + fX*(fC + fX*(fD + fX*fE))); printf("Using Horner's Rule : F(%4.2f) = %4.2f\n", fX ,fX1 ); }