"C Language and Program design" practice reference--sin Taylor Show error

Source: Internet
Author: User
Tags sin

Back to: Teacher He curriculum teaching link project requirements


"Project 1-sin Taylor Show Errors"

Here is the Taylor display of the sin function:
(Note: X takes a Radian value, not an angle value)
A double Mysin (double x) was written for the sin value, but "dead" on the 123 ° (the result of 122° degrees has come out). The loop is no problem (of course the problem is in the loop). Try to use debugging tools to find out where the problem is, and then give a solution to the problem.

#include <stdio.h> #define PI 3.1415926double mysin (double x);d ouble myabs (double x); int main () {    double angle;    for (angle=0; angle<=180; angle++)        printf ("sin (%.0f°) =%.3f\n", Angle, Mysin ((angle/180) *pi));    return 0;} The Mysin function is defined below to find the Sin value double mysin (double x) {    double sum=x,x_pow=x,item;    int n=1,fact=1, sign=1;     The initial value is assigned when the variable is defined, and the first item is taken into account in addition and sum of Do    {        fact=fact* (n+1) * (n+2);  Fact is used to represent factorial, which is the denominator x_pow*=x*x in the formula        ;             X_pow is used to denote factorial in a molecule, and to make denominator sign=-sign in a formula        ;             The symbol that determines the item that will be added is        item =x_pow/fact*sign;//Calculates the item sum+=item to accumulate        ;              Add the item up        n+=2;    } while (Myabs (item) >1e-5);    return sum;} The following defines the Myabs function double Myabs (double x) {    return ((x>=0) x:-x);}
Tip: After entering into the Mysin, notice the changes of each variable to see if the term will converge, thus allowing the loop to end.


[Reference Solution]

(If you need reference, the following tips are open.) If you look at all the tips and then do it, ... Lao He will be sad: Every Jin capsule has painstaking efforts, one to make not easy. )

(If you are in the middle of the implementation, there are also unexpected obstacles to you, please explain in the comments, help Lao he refinement of the bag. )

1: Trace to go into the Mysin function, pay attention to using step into.


2: Tracking the execution of the Mysin function, you can not go into the loop, if you have been using the next line (with step into the same effect when you do not encounter a custom function), click the mouse is monotonous, but also easy to distract attention. Set a breakpoint on a statement in the loop and use the Debug/continue button to track it "across".



3: Because the problem is in the 123°, if you start tracking Mysin in the loop from 0 °, I believe that when you see this balloon, you probably angle not more than 10 (which already shows that you have enough patience). We need to go directly to the call to Mysin when the angle is 123°. There are two ways of doing this:

The first: Change the main function, for example (there are many ways, as long as you can call Mysin (123°) directly):

int main () {    double angle;    printf ("%.3f\n", Mysin ((123/180) *pi));    for (angle=0; angle<=180; angle++)    //    printf ("sin (%.0f°) =%.3f\n", Angle, Mysin ((angle/180) *pi));    return 0;}
The second type: in the Observation window (Watches), you can also change the value of the variable in the trace to see the corresponding execution result. So, for example, before entering into the Mysin function, you can add angle to the observation window (you need to enter the variable name yourself under the variable that automatically appears), and then enter the desired value directly later.

In fact, you can also change the value of X by step into the mysin (or directly into the Mysin via a breakpoint).


Jin SAC 4: It is possible that you listened to the guidance of Lao he, using the first method of the 123°, but found that after entering the Mysin, the value of x is 0, not the value of the radian corresponding to the 3.
Sorry, I dug a hole on purpose, and you came in. Come in, take a seat and go.
The sin value of 123° is not called Mysin ((123/180) *pi), but Mysin ((123.0/180) *PI)! Mysin ((123/180) *pi) is indeed Mysin (0), note the data type.
In this hole, you will know that the modification of the program may introduce new errors. This is a rule in software engineering.


5: Now enter the critical moment (suggest or set the breakpoint tracking), you will find that the problem is that the absolute value of item is gradually reduced to 0 (this reflects the convergence of the Taylor formula, in fact, when the angle value does not reach 123 °, this convergence is guaranteed), but, at some point, The absolute value of item was big again, and the hope of exiting the loop was fading, so that the hope was lost ... You can look at the value of fact, and there are some seemingly incredible things happening here.

Jin sac to this, the phenomenon has been, the following needs your diagnosis. Please try to solve the problem. Solved, or really can't think of it, and then look at the following "truth."



The Truth (upside down, you have to be active, and secondly, you really don't want to make it easy for you to give up your solution to the exploration process):


C language and program design practice reference--sin Taylor display errors

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.