×

Search anything:

Hyperbolic functions in math.h (C/C++)

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

Every programming language supports few functions or libraries or header files which can be directly used to simplify code and programs. Tracking into C and C++,they too have many such functionalities. One such package of mathematical functions in C/C++, resides in the header file named "math.h".

To make it simple lets say your teacher has given you to solve a mathematical problem that involved trignometry,logarithms etc. While you solve, you use a calculator to compute those values (say sin,cos and log) to get the end result.
Have you ever wondered exactly what happens when you type the sine (or cos or tan etc for that matter) of an angle into your calculator? You type it in and it magically gives you an answer, a number that is essentially unrelated to the angle you inputed. Is the calculator just reading off of a list created from people who used rulers to physically measure the distance on a graph or is there a mathematical function that defines it?

So,what are hyperbolic functions? Where and how can we use them exactly?

I'm sure that all of us have solved pretty good equations, yet failed to understand the back story of "Why?". Lets get into it then.

Little Back Story

So for a basic understanding, lets say you and your friend had planned to walk to the movies together, but now it's five minutes to show time and no sign of him. Suddenly there he is, running on the sidewalk in front of your house, and it looks like stopping for you is not on his agenda. You jump off your porch and start to run. Heading towards your friend, you constantly change direction until you catch up.

Looking back at your footsteps, your friend remarks, 'Hey, you traced out an interesting curve.' To which you respond, 'For sure. It's based on hyperbolic functions.' Okay, okay, you probably wouldn't have said this. That is, not until you've completed this article.'Hyperbolic functions occur in all sorts of places.

Usage In C/C++

Hyperbolic functions are defined in terms of exponentials. They're written like the trig functions cosine (cos), sine (sin), tangent (tan), but they have an 'h' at the end. Cosh is pronounced 'kosh;' sinh is pronounced 'sinch;' and tanh is usually read as 'tan h' but sometimes we say 'tanch.'

HEADER FILE :

  • <math.h> (C/C++)
  • cmath (C++)
  • <tgmath.h>

So,these hyperbolic mainly revolve around three special functions. The types of functions are:

  • Complex Hyperbolic Cosine (cosh)
  • Inverse Hyperbolic Cosine (acosh)
  • Complex Inverse Hyperbolic Cosine (cacosh)
  • Complex Hyperbolic Sine (sinh)
  • Inverse Hyperbolic Sine (asinh)
  • Complex Inverse Hyperbolic Sine (casinh)
  • Complex Hyperbolic Tangent (tanh)
  • Inverse Hyperbolic Tangent (atanh)
  • Complex Inverse Hyperbolic Tangent (catanh)

We will, now, go through each of the functions in detail with code examples.

cosh()

In the header: <math.h> and cmath

Datatypes | Functions to be used

  1. Float - coshf(float arg);
  2. Double - cosh(double arg);
  3. Long double - coshl(long double arg);

In the header:<tgmath.h>

#define cosh(arg)

NOTE:

  1. Where is the "int" then? If the argument has integer type or the type double, cosh will be called.
  2. If the argument has type long double, coshl will be called.
  3. If the argument has type float, coshf will be called.

Types of hyperbolic cosine

1. COMPLEX HYPERBOLIC COSINE

In the header file : <math.h>

Datatypes | Functions to be used

  1. Float complex - ccoshf(float complex x);
  2. Double complex - ccoshh(double complex x);
  3. Long double complex - ccoshl(long double complex x);

In the header file : <tgmath.h>

#define<cosh(x)>

Here,'x' is a complex argument.

NOTE:

  1. If x has type long double complex, ccoshl will be called.
  2. If x has type double complex, ccosh will be called.
  3. If x has type float complex, ccosf is called.
  4. If x is real or integer, then the macro invokes the corresponding real function (coshf, cosh, coshl).
  5. If x is imaginary, then the macro invokes the formula cosh(iy) = cos(y), and the return type is real.

2. INVERSE HYPERBOLIC COSINE

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - ccoshf(float complex x);
2.Double complex - ccoshh(double complex x);
3.Long double complex - ccoshl(long double complex x);

In the header file : <tgmath.h>

#define<acosh(arg)>

Here,'arg' is a input value.

NOTE:
1.If the argument has type long double, acoshl is called.
2.Otherwise, if the argument has integer type or the type double, acosh is called. 3.If the argument has float type acoshf is called.
4.If the argument is complex, then the macro invokes the corresponding complex function (cacoshf, cacosh, cacoshl).

3.COMPLEX INVERSE HYPERBOLIC COSINE

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - ccoshf(float complex x);
2.Double complex - ccoshh(double complex x);
3.Long double complex - ccoshl(long double complex x);

In the header file : <tgmath.h>

#define<acosh(x)>

Here,'x' is a complex argument.

NOTE:
1.If x has type long double complex, cacoshl will be called.
2.If x has type double complex, cacosh will be called.
3.If x has type float complex, cacosf is called.
4.If x is real or integer, then the macro invokes the corresponding real function (acoshf, acosh, acoshl).
5.If x is imaginary, then the macro invokes a function whose return type is complex.

CODE IMPLEMENTATION IN C

#include <stdio.h>
#include <math.h>
#include <complex.h>
int main(void)
{
    printf("cosh(1) = %f\ncosh(-1)= %f\n", cosh(1), cosh(-1));
    printf("log(sinh(1) + cosh(1))=%f\n", log(sinh(1)+cosh(1)));
    printf("cosh(+0) = %f\ncosh(-0) = %f\n", cosh(0.0), cosh(-0.0));
    
    double complex z = ccosh(1);  // behaves like real cosh along the real line
    printf("cosh(1+0i) = %f%+fi (cosh(1)=%f)\n", creal(z), cimag(z), cosh(1));
    
    printf("acosh(1) = %f\nacosh(10) = %f\n", acosh(1), acosh(10));
    printf("acosh(0.5) = %f\n", acosh(0.5)); 
    
    double complex z = cacosh(0.5);
    printf("cacosh(+0.5+0i) = %f%+fi\n", creal(z), cimag(z));
}

OUTPUT :

    cosh(1) = 1.543081
    cosh(-1)= 1.543081
    log(sinh(1) + cosh(1))=1.000000
    cosh(+0) = 1.000000
    cosh(-0) = 1.000000
    cosh(1+0i) = 1.543081+0.000000i (cosh(1)=1.543081)
    acosh(1) = 0.000000
    acosh(10) = 2.993223
    acosh(0.5) = -nan
    cacosh(+0.5+0i) = 0.000000-1.047198i

2.sinh()

In the header: <math.h> and cmath
Datatypes | Functions to be used
1.Float - sinhf(float arg);
2.Double - sinh(double arg);
3.Long double - sinhl(long double arg);

In the header: <tgmath.h>
"#define sinh(arg)"

NOTE:
1.If the argument has integer type or the type double, sinh will be called.
2.If the argument has type long double, sinhl will be called.
3.If the argument has type float, sinhf will be called.

TYPES OF HYPERBOLIC SINE

1.COMPLEX HYPERBOLIC SINE

In the header file : <math.h>

Datatypes | Functions to be used

  1. Float complex - csinhf(float complex x);
  2. Double complex - csinhh(double complex x);
  3. Long double complex - csinhl(long double complex x);

In the header file : <tgmath.h>

#define<sinh(x)>

Here,'x' is a complex argument.

NOTE:
1.If x has type long double complex, csinhl will be called.
2.If x has type double complex, csinh will be called.
3.If x has type float complex, csinf is called.
4.If x is real or integer, then the macro invokes the corresponding real function (sinhf, sinh, sinhl).
5.If x is imaginary, then the macro invokes the formula sinh(iy) = sin(y), and the return type is real.

2.INVERSE HYPERBOLIC SINE

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - asinhf(float complex x);
2.Double complex - asinh(double complex x);
3.Long double complex - asinhl(long double complex x);

In the header file : <tgmath.h>

#define<asinh(arg)>

Here,'arg' is a input value.

NOTE:
1.If the argument has type long double, asinhl is called.
2.Otherwise, if the argument has integer type or the type double, asinh is called. 3.If the argument has float type asinhf is called.
4.If the argument is complex, then the macro invokes the corresponding complex function (casinhf, casinh, casinhl).

3.COMPLEX INVERSE HYPERBOLIC SINE

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - casinhf(float complex x);
2.Double complex - casinh(double complex x);
3.Long double complex - casinhl(long double complex x);

In the header file : <tgmath.h>

#define<asinh(x)>

Here,'x' is a complex argument.

NOTE:
1.If x has type long double complex, casinhl will be called.
2.If x has type double complex, casinh will be called.
3.If x has type float complex, casinhf is called.
4.If x is real or integer, then the macro invokes the corresponding real function (asinhf, asinh, asinhl).
5.If x is imaginary, then the macro invokes a function whose return type is complex.

CODE IMPLEMENTATION IN C

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

int main(void)
{
    printf("sinh(1) = %f\nsinh(-1)=%f\n", sinh(1), sinh(-1));
    printf("log(sinh(1) + cosh(1))=%f\n", log(sinh(1)+cosh(1)));
    printf("sinh(+0) = %f\nsinh(-0)=%f\n", sinh(0.0), sinh(-0.0));
    printf("sinh(710.5) = %f\n", sinh(710.5)); #results in error
    
    double complex z = csinh(1);  // behaves like real sinh along the real line
    printf("sinh(1+0i) = %f%+fi (sinh(1)=%f)\n", creal(z), cimag(z), sinh(1));
    
    printf("asinh(1) = %f\nasinh(-1) = %f\n", asinh(1), asinh(-1));
    printf("asinh(+0) = %f\nasinh(-0) = %f\n", asinh(0.0), asinh(-0.0));
    
    double complex z = casinh(0+2*I);
    printf("casinh(+0+2i) = %f%+fi\n", creal(z), cimag(z));    
 }

OUTPUT :

sinh(1) = 1.175201
sinh(-1)=-1.175201
log(sinh(1) + cosh(1))=1.000000
sinh(+0) = 0.000000
sinh(-0)=-0.000000
sinh(710.5) = inf
sinh(1+0i) = 1.175201+0.000000i (sinh(1)=1.175201)
asinh(1) = 0.881374
asinh(-1) = -0.881374
asinh(+0) = 0.000000
asinh(-0) = -0.000000
casinh(+0+2i) = 1.316958+1.570796i

3.tanh()

In the header: <math.h> and cmath
Datatypes | Functions to be used
1.Float - tanhf(float arg);
2.Double - tanh(double arg);
3.Long double - tanhl(long double arg);

In the header: <tgmath.h>

#define<tanh(arg)>

NOTE:
1.Where is the "int" then? If the argument has integer type or the type double, tanh will be called.
2.If the argument has type long double, tanhl will be called.
3.If the argument has type float, tanhf will be called.

TYPES OF HYPERBOLIC TANGENTS

1.COMPLEX HYPERBOLIC TANGENT

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - ccoshf(float complex x);
2.Double complex - ccoshh(double complex x);
3.Long double complex - ccoshl(long double complex x);

In the header file : <tgmath.h>

#define<cosh(x)>

Here,'x' is a complex argument.

NOTE:
1.If x has type long double complex, ctanhl will be called.
2.If x has type double complex, ctanh will be called.
3.If x has type float complex, ctanhf is called.
4.If x is real or integer, then the macro invokes the corresponding real function (tanhf, tanh, tanhl).
5.If x is imaginary, then the macro invokes the formula tanh(iy) = tan(y), and the return type is real.

2.INVERSE HYPERBOLIC TANGENT

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - atanhf(float complex x);
2.Double complex - atanh(double complex x);
3.Long double complex - atanhl(long double complex x);

In the header file : <tgmath.h>

#define<atanh(arg)>

Here,'arg' is a input value.

NOTE:
1.If the argument has type long double, atanhl is called.
2.Otherwise, if the argument has integer type or the type double, atanh is called. 3.If the argument has float type atanhf is called.
4.If the argument is complex, then the macro invokes the corresponding complex function (catanhf, catanh, catanhl).

3.COMPLEX INVERSE HYPERBOLIC TANGENT

In the header file : <math.h>
Datatypes | Functions to be used
1.Float complex - catanhf(float complex x);
2.Double complex - catanh(double complex x);
3.Long double complex - catanhl(long double complex x);

In the header file : <tgmath.h>

#define<atanh(x)>

Here,'x' is a complex argument.

NOTE:
1.If x has type long double complex, catanhl will be called.
2.If x has type double complex, catanh will be called.
3.If x has type float complex, catanf is called.
4.If x is real or integer, then the macro invokes the corresponding real function (atanhf, atanh, atanhl).
5.If x is imaginary, then the macro invokes a function whose return type is complex.

Code implementation in C

#include <stdio.h>
#include <math.h>
#include <complex.h>
 
int main(void)
{
    printf("tanh(1) = %f\ntanh(-1) = %f\n", tanh(1), tanh(-1));
    printf("tanh(0.1)*sinh(0.2)-cosh(0.2) = %f\n", 
    tanh(0.1) * sinh(0.2) -  cosh(0.2));
    printf("tanh(+0) = %f\ntanh(-0) = %f\n", tanh(0.0), tanh(-0.0));
        
    double complex z = ctanh(1);  // behaves like real tanh along the real line
    printf("tanh(1+0i) = %f%+fi (tanh(1)=%f)\n", creal(z), cimag(z), tanh(1));
    
    printf("atanh(0) = %f\natanh(-0) = %f\n", atanh(0), atanh(-0.0));
    printf("atanh(0.9) = %f\n", atanh(0.9));
    printf("atanh(-1) = %f\n", atanh(-1)); #generates error
    
    double complex z = catanh(2);
    printf("catanh(+2+0i) = %f%+fi\n", creal(z), cimag(z));
 } 

OUTPUT :

tanh(1) = 0.761594
tanh(-1) = -0.761594
tanh(0.1)*sinh(0.2)-cosh(0.2) = -1.000000
tanh(+0) = 0.000000
tanh(-0) = -0.000000
tanh(1+0i) = 0.761594+0.000000i (tanh(1)=0.761594)
atanh(0) = 0.000000
atanh(-0) = -0.000000
atanh(0.9) = 1.472219
atanh(-1) = -inf
catanh(+2+0i) = 0.549306+1.570796i

Test your knowledge

Question 1

If the argument value is an integer, which of the following functions must be used to compute the inverse hyperbolic cosine?

a.cosh(double arg)
b.cosh(arg)
c.acosh(double arg)
d.acosh(arg)
Answer: Option c

Question 2

Which header file is the combination of math.h and complex.h?

a."tgath.h"
b."tgmath.h"
c."cmath"
d.All of the above
Answer: Option b

With this article at OpenGenus, you will have a strong understanding of Hyperbolic functions in math.h (C and C++). Enjoy.

Hyperbolic functions in math.h (C/C++)
Share this