我对编程还很陌生.我知道这段代码看起来也不是很好的设计.如果我不应该问这样的问题,请让我知道.我会把它记下来,然后自己解决问题(希望如此).

我的第一个使用泰勒级数计算ex的程序运行良好:

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

double calculateTaylorSeries(double x, int n) {
    double result = 1.0; // The first term in the series

    double term = 1.0;
    for (int i = 1; i <= n; i++) {
        term *= x / i; 
        result += term; 
    }

    return result;
}

int main(void) {
    double x;

    printf("Enter the value of x: ");
    scanf("%lf", &x);

    int n;
    printf("Enter the number of terms (n): ");
    scanf("%d", &n);

    double taylorApproximation = calculateTaylorSeries(x, n);

    printf("e^%.2lf (approximated using %d terms): %lf\n", x, n, taylorApproximation);

    return 0;
}

但对sin函数做同样的事情似乎很困难.因为有一个交替的术语方面.而权力和分母必须是奇数.

我的逻辑在我看来是正确的.但它给了我不准确的结果.

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

double calculateTaylorSeries(double x, int n)
{
    double result = x;
    double term = 1.0;
    int sign = -1;
    for (int i = 1; i <= 2 * n; i++)
    {
        if (i % 2 == 1)
        {
            term *= x / i * sign; //term = term * x/i * sign;

            result += term; // result = result + term;
        }
    }

    return result; 
}

int main(void)
{
    double x; 
    printf(" Enter the angle : ");

    scanf("%lf", &x);
    
    int n;
    
    printf("Enter the number of terms(n):");
    scanf("%d", &n);

    double sinX = calculateTaylorSeries(x, n);

    printf("sin(%lf) = %lf", x, sinX);

    return 0;
}

以下是输出:

Enter the angle : 6.28
Enter the number of terms(n):2
sin(6.280000) = 13.146133
Enter the angle : 234
Enter the number of terms(n):15 
sin(234.000000) = -49592774856964366336.000000

我还搞不懂为什么在VS代码中运行这段代码时,它会显示以下错误:

The preLaunchTask 'C/C++:gcc.exe build active file' terminated with exit code -1.

推荐答案

你的学期计算中有两个错误:

  • 您不能交替使用手势
  • 您可以跳过术语计算中的偶数.你应该只在总和中略过它们.

以下是修改后的版本:

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

double calculateTaylorSeries(double x, int n) {
    double result = x;
    double term = 1.0;

    for (int i = 1; i <= 2 * n; i++) {
        term *= x / i;
        if (i % 2 == 1) {
            term = -term;
            result += term; // result = result + term;
        }
    }
    return result; 
}

int main(void) {
    double x; 
    printf("Enter the angle in radians: ");
    if (scanf("%lf", &x) != 1)
        return 1;
    
    int n;
    printf("Enter the number of terms(n):");
    if (scanf("%d", &n) != 1)
        return 1;

    double sinX = calculateTaylorSeries(x, n);
    printf("sin(%lf) = %lf", x, sinX);
    return 0;
}

C++相关问答推荐

为指针 struct 创建宏

你能用自己的地址声明一个C指针吗?

POSIX文件描述符位置

增加getaddrinfo返回的IP地址数量

为什么在4.9.37版的内核中,kfio还需要smp_wmb呢?

SDL 2.0-从数组渲染纹理

进程在写入管道时挂起

ifdef __cplusplus中的整数文字单引号

使用nmake for程序比Hello World稍微复杂一些

C语言中的外部关键字

为什么realloc函数在此代码中修改变量?

C语言中神秘的(我认为)缓冲区溢出

C-try 将整数和 struct 数组存储到二进制文件中

在运行时判断C/C++指针是否指向只读内存(在Linux操作系统中)

为什么会出现此错误?二进制表达式的操作数无效

计算时出现奇怪的计算错误;N Select K;在C中

与指针的原始C数组或C++向量<;向量<;双>>;

指向返回 struct 成员的指针,安全吗?

const struct 成员的 typedef 中的灵活数组大小

在 C 中传递参数时出现整数溢出