Horner's rule-exercise solution 5 of mad man C (Chapter 2 Exercise 8)

Source: Internet
Author: User
/* Chapter 2 8. calculate the approximate polynomial value x ^ 4 + 2x ^ 3 + 3x ^ 2 + 8x + 7, x = 3.47 */# include <stdio. h> # include <stdlib. h> # define x 3.47int main (void) {printf ("% F \ n", (x + 2 .) * x + 3 .) * x + 8 .) * x + 7 .); system ("pause"); Return 0 ;}

The first mistake that beginners can easily make on this topic is to write the following expression to evaluate it.

X^4+2*X^3+3*X^2+8*X+7

The error in this expression is that "^" is treated as a power calculation by mistake. However, in C language, there is no power calculation. Generally, the power is obtained.

Is represented by multiplication.
The more serious problem is that "^" is indeed an operation in the C language, so the above expression may be mistakenly recognized by the C Language

It is a correct expression (when X is an integer), but the meaning of the operation is not a power.
Another easy mistake for beginners to make on this question is to forget to write "*", such
X + 2x X + 3x * x + 8x + 7
This brings the habit of writing expressions in algebra into the code. The rules for writing expressions in C language and the rules for algebraic expressions are as follows:

Fundamentally different. Which of the following statements is true?
X + 2 x + 3 x + 8 x + 7
Although there is no problem with this writing method, most beginners do not realize the true meaning of this expression.

It actually means
X * x + (double) 2 * x + (double) 3 * x + (double) 8 * x + (double) 7
If you are not clear about data types, you will never learn programming well.
X + 2 x + 3 x + 8 x + 7
If the true meaning of such an expression is true, try not to write an expression that combines the integer type and the floating point type. For beginners

It is best to write the above expression
X * x + 2. * x + 3. * x + 8. * x + 7.
Or
X * x + (double) 2 * x + (double) 3 * x + (double) 8 * x + (double) 7
This shows that you know exactly what you want to do is a floating point arithmetic operation.
However
X * x + 2. * x + 3. * x + 8. * x + 7.
Perform 9 multiplication operations and 4 addition operations, and organize the original table according to the Horner's rule rules.

If the formula is used (see the code), the calculation amount is reduced to three multiplication operations and four addition operations. For large-scale computation

The computing time is amazing.
The Horner rule is a method used to reduce the computational workload when calculating polynomial values.
X ^ 4 + 2x ^ 3 + 3x ^ 2 + 8x + 7
Such polynomials are organized
(1x + 2) x + 3) x + 8) x + 7
The significance of this form is to "rule" the operation, which is even more important for learning to write code, because

It unifies the methods for finding polynomial values.

/* Chapter 2 8. calculate the approximate polynomial value x ^ 4 + 2x ^ 3 + 3x ^ 2 + 8x + 7, x = 3.47 */# include <stdio. h> # include <stdlib. h> # define x 3.47int main (void) {Double Y = 1 .; y * = x; y + = 2 .; y * = x; y + = 3 .; y * = x; y + = 8 .; y * = x; y + = 7 .; printf ("% F \ n", Y); System ("pause"); Return 0 ;}

To learn programming well, understanding these basic ways of thinking is extremely important.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.