cosh() without math.c library in C language

150 Views Asked by At

I need to calculate the cosh(x), but I can not use the math.c library, any idea how can I solve it? This is in C language.

1

There are 1 best solutions below

3
AbdelAziz AbdelLatef On

You can use Taylor Series to represent 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;
    }
}