没有cmath库的余弦计算

Cosine Calculation without cmath library

本文关键字:余弦 计算 cmath 没有      更新时间:2023-10-16

我是一名计算机科学专业的学生。我被分配了一个项目,我需要使用C++创建一个小程序,该程序将询问度数或弧度,然后输出sin、cos和tan值,但我只能使用#include <iostream>。正弦值工作正常,但余弦值是个问题。这是我用来计算的代码:

float rad = radian value;
float func_cos (float rad)
{
    float cos;
    int i = 0;
    float sum = 0;
    float x = rad;
    while (fabs(x) > 0.000001)
    {
        i = i + 2;
        x = -(x) * ((rad*rad)/(i*(i-1)));
        sum = (sum) + (x);
    }
    cos = 1 - sum;
    return cos;
}   

您有sin(x)工作吗?那就很容易了。cos(x) = sin(x + pi/2)

我同意"Oli Charlesworth","fabs(x)"函数不在"iostream"标头中。