ccoosshh() -- Mathematics Function (libm)

Calculate hyperbolic cosine
#iinncclluuddee <mmaatthh.hh>
ddoouubbllee ccoosshh(_r_a_d_i_a_n) ddoouubbllee _r_a_d_i_a_n;

ccoosshh()  calculates the  hyperbolic  cosine of  _r_a_d_i_a_n, which  is in  radian
measure.

_E_x_a_m_p_l_e
The following example uses ccoosshh() to  compute the height and time to impact
of a falling object.  Assume that an object is acted on both by gravity and
by air  resistance propotional to _v829,  where _v is its  velocity.  When _p is
the proptionality  constant for the resistance of  air, the object's height
after _t seconds is given by the formula

    y = y0 -1/p*ln(cosh(t*sqrt(p*g)))

and its time to reach the ground is given by the formula:

    t = 1/sqrt(p*g)*log(exp(p*y0)+sqrt(exp(2*p*y0)-1))

Assuming that

    g = 32 ft/s82



8the example computes an object's height  after _t seconds and the total time
in seconds that it will take to reach the ground.

This example was written by Sanjay Lals (lals@skule.ecf.toronto.edu).

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

main ()
{
    float height, init_height, resistance, time_to_hit, g;
    int i;
    char buffer[50];

    g = 32.0;

    printf("Enter initial height, in feet: ");
    fflush(stdout);
    init_height = atof(gets(buffer));

    resistance = 0.0;
    while (resistance > 0.005 || resistance < 0.001) {
        printf("Enter air resistance (0.001 to 0.005): ");
        fflush(stdout);
        resistance = atof(gets(buffer));
    }

    time_to_hit = 1.0/sqrt(resistance*g) * log(exp(resistance*init_height) + sqrt(exp(2*resistance*init_height)-1));

    printf("Initial height: %1.0f\n", init_height);
    printf("Air resistance: %1.3f\n", resistance);
    printf("Time for object to hit the ground: %1.3f seconds\n",
        time_to_hit);

    /* countdown to impact */
    for (i = 2; ; i++) {
        height = init_height -
            (1.0/resistance*log(cosh(sqrt(resistance*g)*(double)i)));

        if (height < 0) {
            printf("BOOM!\n");
            exit(EXIT_SUCCESS);
        } else
            printf("Height after %i seconds: %1.3f feet\n", i, height );
    }
}

_S_e_e _A_l_s_o
lliibbmm, ccooss()
ANSI Standard, section 7.5.3.1
POSIX Standard, section 8.1

_D_i_a_g_n_o_s_t_i_c_s
When overflow occurs, ccoosshh() returns a huge value that has the same sign as
the actual result.
