C 语言中没有 math.c 库的 cosh()

问题描述

我需要计算 cosh(x),但我不能使用 math.c 库,知道如何解决吗? 这是用C语言写的。

解决方法

您可以使用 Taylor Seriesrepresent cosh

cosh(x) = 1 + x ^ 2 / 2! + x ^ 4 / 4! + x ^ 6 / 6! + x ^ 8 / 8! + ....

double cosh(double x)
{
    double c,f,xp;
    c = f = xp = 1;
    for (int i = 1; i < 10; i++) // You can increase the number of terms to get better precision
    {
        f *= (2 * i - 1) * (2 * i);
        xp *= x * x;
        c += xp / f;
    }
}