简体   繁体   English

编写 C function 来评估系列 // sin(x) = x-(x3 /3!)+(x5 /5!)-(x7 /7!)+... // 最多 10 个术语

[英]Write a C function to evaluate the series // sin(x) = x-(x3 /3!)+(x5 /5!)-(x7 /7!)+… // up to 10 terms

#include <stdio.h>
#include<math.h>

int series(float,float);

int main()

{

    float x,n,series_value;
    printf("Enter the value of x: ");
    scanf("%f",&x);
    printf("\nEnter the value of n: ");
    scanf("%f",&n);
    series_value=series(x,n);
    printf("\nValue of series sin (%.2f) is: %f\n",x,series_value);
    return 0;
}

int series(float x,float n)

{

    int i,sum=0,sign=-1;
    int j,fact=1,p=1;
    for (i=1; i<=(2*n)-1; i+=2)
    {
        for (j=1; j<=i; j++)
        {
        p=p*x;
        fact=fact*j;
        }
        sign=-1*sign;
        sum=sum + sign*p/fact;       
    }
    return (sum);
}

Output: Enter the value of x: 5 Output:输入x的值:5

Enter the value of n: 10 (lldb) and this message Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0) :[Thread 1 Queue.输入 n 的值:10 (lldb) 和此消息Thread 1: EXC_ARITHMETIC (code=EXC_I386_DIV, subcode=0x0) :[Thread 1 Queue. com.apple.main-thread (serial) ] 1 com.apple.main-thread(串行)] 1

Why is this message coming?为什么会出现这条消息? and what is wrong in the program as answer is not coming right程序出了什么问题,因为答案不正确

There is a few problems with your code.您的代码存在一些问题。 As @PaulHankin said, when fact overflows and becoms zero, you will have a division by zero, and "weird things" happen.正如@PaulHankin 所说,当fact溢出并变为零时,您将被零除,并且发生“奇怪的事情”。

Your factorial and power calculation is also wrong.您的阶乘和功率计算也是错误的。 You are recalculating it in each iteration of the outer loop without reseting fact and p first:您在外循环的每次迭代中重新计算它,而不首先重置factp

fact = 1; // You need to reset fact and p to its start value here
p = 1;
for (j=1; j<=i; j++)
  {
    p=p*x;
    fact=fact*j;
  }

Your third problem is that for your function calculate the correct value for sin , which is not an integer value, you need to use float , or even better double , when calculating sum .您的第三个问题是,对于您的 function 计算sin的正确值,这不是 integer 值,您需要在计算sum时使用float甚至更好的double So sum must be declared float , and the division p/fact must use float division.所以sum必须声明为float ,并且除法p/fact必须使用浮点除法。 By also declaring p and fact as float, you will solve both the overflow issue, and use the correct division.通过还将pfact声明为 float,您将解决溢出问题并使用正确的除法。 Naturally your function must also return a float自然你的 function 也必须返回一个float

float series(float x,float n)
{
    int i,sign=-1;
    int j, 
    float sum = 0;
    float fact = 1;
    float p = 1;
    for (i=1; i<=(2*n)-1; i+=2)
    {
        fact = 1;
        p = 1;
        for (j=1; j<=i; j++)
        {
           p=p*x;
           fact=fact*j;
        }
        sign=-1*sign;
        sum=sum + sign*p/fact;       
    }
    return (sum);
}

This code still has a minor problem.这段代码还有一个小问题。 By having an inner loop, it is slower than necessary.通过有一个内部循环,它比必要的要慢。 Since this probably is homework, I am not getting rid of that loop for you, just giving you a hint: You don't have to recalculate fact from scratch on each iteration of the outer loop, just try to find out how fact changes from one iteration to the next.由于这可能是家庭作业,我不会为您摆脱那个循环,只是给您一个提示:您不必在外部循环的每次迭代中从头开始重新计算fact ,只需尝试找出fact如何从一次迭代到下一次。 The same goes for p . p也是如此。

//Series of Sinx

#include<stdio.h>

#include<math.h>

#define ACCURACY 0.0001

int factorial(int n);

int main()
{
    float x,sum,term;
    int i,power;

    printf("Enter value of X: ");
    scanf("%f",&x);

    i=1;
    power=3;
    sum=x;
    term=x;

    while(term>=ACCURACY)
    {
        term = pow(x,power) / factorial(power);

        if(i%2==1)
        {
            sum -= term;
        }
        else
        {
            sum += term;
        }

        power+=2;
        i++;
    }
    printf("sin(%f) = %.6f\n",x,sum);
    
    return 0;
}

int factorial(int n){
    int i=n,fact=1;
    for(i=1;i<=n;i++)
    {
        fact=fact*i;
    }
    return fact;
}

plenty bugs.很多错误。 To do not caclulate the fact values all the time they are in the lookup table不要在查找表中始终计算事实值

#include <stdio.h>
#include <math.h>

double series(double,int);

long long fact[] = { 1, 2, 6, 24,
                     120, 720, 5040, 40320,
                     362880, 3628800, 39916800, 479001600,
                     6227020800, 87178291200, };

double mypow(double x, unsigned p)
{
    double result = x;
    while(p && --p)
        result *= x;
    return result;
}

int main()
{
    for(double x = 0; x <= M_PI + M_PI / 60; x += M_PI / 30)
        printf("Value of series sin (%.2f) is: %f\n",x,series(x, 5));
    fflush(stdout);
}


double series(double x,int n)
{
    double sum = x;
    int i,sign=1;
    for (i=3; i<=(2*n)-1; i+=2)
    {
        sign=-1*sign;
        sum += sign*(mypow(x, i)/fact[i -1]);
    }
    return (sum);
}

https://godbolt.org/z/U6dULN https://godbolt.org/z/U6dULN

maybe its due to floating-point exception as u have declared that the function should return int type value int series(float,float);//hear so u can try editing the return type of this function as float Note:-also u need to change at function definition and the datatype of int i,sum=0,sign=-1;可能是由于浮点异常,因为你已经声明 function 应该返回 int 类型值 int series(float,float);//所以你可以尝试编辑这个 function 的返回类型作为浮点注意:-你也需要更改 function 定义和int i,sum=0,sign=-1 的数据类型; int j,fact=1,p=1;诠释j,事实 = 1,p = 1; to float as it is returning the value (sum) which should also be float浮动,因为它返回的值(总和)也应该是浮动的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM