×

Search anything:

Trigonometric Functions using <math.h> in C

Binary Tree book by OpenGenus

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

The math.h header defines various mathematical functions including trigonometric and hyperbolic functions. All the these predefined trigonometric functions use radians as argument. We have explored all Trigonometric Functions using <math.h> in C.

So what's the deal with radians?

There are two forms of measurement for the the arc of a circle:

  1. Degrees
  2. Radians

for example: 180° is equal π rad - a half of the circle (180°= π)

CONVERSION OF DEGREES TO RADIANS
one degree equals 0.01745329 radians

#include <stdio.h>
int main()
{
   float degrees,radians;
   printf("Enter the angle in degrees :");
   scanf("%f",&degrees);
   radians = 0.0174532925*degrees;
   printf("%.2f degrees = %f radians",degrees,radians);
   return(0);
}

CONVERSION OF RADIANS TO DEGREES
one radian equals 57.2957795 degrees

#include <stdio.h>
int main()
{
   float degrees,radians;
   printf("Enter the angle in radians :");
   scanf("%f",&radians);
   degrees = 57.2957795*radians;
   printf("%f radians = %.2f degrees",radians,degrees);
   return(0);
}

LIST OF INBUILT TRIGONOMETRIC FUNCTIONS IN MATH.H FILE:



Trigonometric functions-

FUNCTIONS
sin() computes sine of an angle
sinf() ( sin(arg) )
sinl()
cos() computes cosine of an angle
cosf() ( cos(arg) )
cosl()
tan() computes tangent of an angle
tanf() ( tan(arg) )
tanl()
asin() computes arc sine of an angle
asinf() ( asin(arg) )
asinl()
acos() computes arc cosine of an angle
acosf() ( acos(arg) )
acosl()
atan() computes arc tangent of an angle
atanf() ( atan(arg) )
atanl()
atan2() computes arc tangent, using signs to
atan2f() determine quadrants
atan2l()

Hyperbolic functions-

FUNCTIONS
sinh() computes hyperbolic sine of an angle
sinhf() ( sinh(arg) )
sinhl()
cosh() computes hyperbolic cosine of an angle
coshf() ( cosh(arg) )
coshl()
tanh() computes hyperbolic tangent of an angle
tanhf() ( tanh(arg) )
tanhl()
asinh() computes inverse hyperbolic sine of an angle
asinhf() ( asinh(arg) )
asinhl()
acosh() computes inverse hyperbolic cosine of an angle
acoshf() ( acosh(arg) )
acoshl()
atanh() computes inverse hyperbolic tangent of an angle
atanhf() ( atanh(arg) )
atanhl()

sin(), sinf(), sinl()

The sin() function returns the sine of an argument (argument in radians).

double sin( double arg );

If the argument has type int or the type double, sin is called.

float sinf( float arg );

If the argument has type float , sinf is called.

(Since C99)

long double sinl( long double arg );

If the argument has type long double , sinl is called.

(Since C99)

Return value

The return value of sin() lies between +1 and -1
RANGE: [-1 ; +1]

  • if the argument is ±∞, NaN is returned
  • if the argument is NaN, NaN is returned

Example 1

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

int main()
{
    double arg;
    double result;
    
    arg = -5.34756;
    result = sin(arg);
    printf("sin(%f) = %f\n", arg, result);

    arg = 0;
    result = sin(arg);
    printf("sin(%.2lf) = %.2lf\n", arg, result);

    arg = INFINITY;
    result = sin(arg);
    printf("sin(%.2lf) = %.2lf\n", arg, result);
    
    return 0;
}

Output 1

sin(-5.347560) = 0.804970   
sin(0.00) = 0.00            
sin(inf) = -nan

cos(), cosf(), cosl()

The cos function returns the cosine of an argument (argument in radians).

double cos( double arg );

If the argument has type int or the type double, cos is called.

float cosf( float arg );

If the argument has type float , cosf is called.

(Since C99)

long double cosl( long double arg );

If the argument has type long double , cosl is called.

(Since C99)

Return value

The return value of cos() lies between +1 and -1
RANGE: [-1 ; +1]

  • if the argument is ±∞, NaN is returned
  • if the argument is NaN, NaN is returned

Example 2

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

int main()
{
   double arg;
   double result;

   arg = 0.523599;
   result = cos(arg);
   printf("cos(%.2lf) = %.2lf\n", arg, result);
   
   arg = +0;
   result = cos(arg);
   printf("cos(%.2lf) = %.2lf\n", arg, result);
   
   arg = -1;
   result = cos(arg);
   printf("cos(%.2lf) = %.2lf\n", arg, result);

   return 0;
}

Output 2

cos(0.52) = 0.87         
cos(0.00) = 1.00   
cos(-1.00) = 0.54

tan(), tanf(), tanl()

The tan function returns the tangent of an argument (argument in radians).

double tan( double arg );

If the argument has type int or the type double, tan is called.

float tanf( float arg );

If the argument has type float , tanf is called.

(Since C99)

long double tanl( long double arg );

If the argument has type long double , tanl is called.

(Since C99)

Example 3

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

int main()
{
    double x;
    double result;

    x = 3.78;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n", x, result);

    x = -1.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n", x, result);

    x = -0;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n", x, result);

    return 0;
}

Output 3

tan(3.78) = 0.74  
tan(-1.30) = -3.60  
tan(0.00) = 0.00

asin(), asinf(), asinl()

The asin() function returns the arc/inverse sine of an angle in radians, it takes a single argument (1 ≥ arg ≥ -1), and returns the arc sine in radians.

double asin( double arg );

If the argument has type int or the type double, asin is called.

float asinf( float arg );

If the argument has type float , asinf is called.

(Since C99)

long double asinl( long double arg );

If the argument has type long double , asinl is called.

(Since C99)

Parameter

The asin() function argument should be in the range of -1 and +1.
DOMAIN: [-1, +1]

Return value

The return value of asin() lies between -Ï€/2 and +Ï€/2 in radians
RANGE: [-Ï€/2, +Ï€/2]

  • If arg > 1 or arg < -1, a domain error occurs and NaN is returned.
  • if the argument is NaN, NaN is returned

Example 4

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

int main()
{  
    double x;
    double result;

    arg =  -1;
    result = asin(arg);
    printf("Inverse of sin(%.2f) = %.2lf in radians\n", arg, result);
    
    // argument not in range
    arg = 1.2;
    result = asin(arg);
    printf("Inverse of sin(%.2f) = %.2lf", arg, result);

    return 0;
}

Output 4

Inverse of sin(-1.00) = -1.57 in radians          
Inverse of sin(1.20) = nan 

acos(), acosf(), acosl()

The acos() function returns the arc/inverse cosine of a number in radians, it takes a single argument in the range (1 ≥ arg ≥ -1)

double acos( double arg );

If the argument has type int or the type double, acos is called.

float acosf( float arg );

If the argument has type float , acosf is called.

(Since C99)

long double acosl( long double arg );

If the argument has type long double , acosl is called.

(Since C99)

Parameter

The acos() function argument should be in the range of -1 and +1.
DOMAIN: [-1, +1]

Return value

The return value of acos() lies between 0 and π in radians
RANGE: [0.0, π]

  • If arg > 1 or arg < -1, a domain error occurs and NaN is returned.
  • if the argument is NaN, NaN is returned

Example 5

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

int main()
{
    double arg;
    double result;

    arg =  -0.0;
    result = acos(arg);
    printf("Inverse of cos(%.2f) = %.2lf in radians\n", arg, result);

    arg =  0.5;
    result = acos(arg);
    printf("Inverse of cos(%.2f) = %.2lf in radians\n", arg, result);

    // argument not in range
    arg = 2.58;
    result = acos(arg);
    printf("Inverse of cos(%.2f) = %.2lf", arg, result);

    return 0;
}

Output 5

Inverse of cos(-0.00) = 1.57 in radians    
Inverse of cos(0.50) = 1.05 in radians     
Inverse of cos(2.58) = nan

atan(), atanf(), atanl()

The atan() function returns the arc/inverse tangent of a number in radians.

double atan( double arg );

If the argument has type int or the type double, atan is called.

float atanf( float arg );

If the argument has type float , atanf is called.

(Since C99)

long double atanl( long double arg );

If the argument has type long double , atanl is called.

(Since C99)

Parameter

The atan() function argument can have any value between +∞ and -∞

Return value

The return value of atan() lies between -Ï€/2 and +Ï€/2 in radians
RANGE: [-Ï€/2, +Ï€/2]

  • If the argument is +∞, +Ï€/2 is returned
  • If the argument is -∞, -Ï€/2 is returned
  • If the argument is NaN, NaN is returned

Example 6

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

int main()
{
    double arg;
    double result;

    arg= INFINITY;
    result = atan(arg);
    printf("Inverse of tan(%.2f) = %.2f in radians\n",arg, result);
    
    arg= 5.67;
    result = atan(arg);
    printf("Inverse of tan(%.2f) = %.2f in radians",arg, result);

    return 0;
}

Output 6

Inverse of tan(inf) = 1.57 in radians 
Inverse of tan(5.67) = 1.40 in radians

atan2(), atan2f(), atan2l()

The function atan2() takes two arguments: x and y coordinates.
It computes the arc tangent of y/x and calculates the angle in radians to determine the correct quadrant.

double atan2( double y, double x );

If the argument has type int or the type double, atan2 is called.

float atan2f( float y, float x );

If the argument has type float , atan2f is called.

(Since C99)

long double atan2l( long double y, long double x );

If the argument has type long double , atan2l is called.

(Since C99)

Return value

The return value of atan2() lies in [-Ï€ ; +Ï€] radians

  • If x is +∞ and y is finite and negative, -0 is returned
  • If x is +∞ and y is finite and positive, +0 is returned
  • If x is -∞ and y is finite and positive, +Ï€ is returned
  • If x is -∞ and y is finite and negative, -Ï€ is returned
  • If y is ±0 and x is negative or -0, ±π is returned
  • If y is ±0 and x is positive or +0, ±0 is returned
  • If either x is NaN or y is NaN, NaN is returned

Example 7

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

int main()
{
    double x, y, result;

    y = -6.5;
    x = 4.8;
    result = atan2(y, x);
    printf(" atan2(y = %f, x = %f) = %f radians\n", y, x, result);

    y = -5.67;
    x = - INFINITY;
    result = atan2(y, x);
    printf(" atan2(y = %f, x = %f) = %f radians\n", y, x, result);
    return 0;
}

Output 7

 atan2(y = -6.500000, x = 4.800000) = -0.934721 radians 
 atan2(y = -5.670000, x = -inf) = -3.141593 radians

sinh(), sinhf(), sinhl()

The sinh() function returns the hyperbolic sine of an argument (argument in radians)

double sinh( double arg );

If the argument has type int or the type double, sinh is called.

float sinhf( float arg );

If the argument has type float , sinf is called.

(Since C99)

long double sinhl( long double arg );

If the argument has type long double , sinhl is called.

(Since C99)

Return value

The return value of sinh() lies between +∞ and -∞

  • If the argument is ±0 or ±∞, it is returned unmodified
  • If the argument is NaN, NaN is returned

Example 8

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

int main()
{
    double arg;
    double result;
    
    arg=-6.78;
    result = sinh(arg);
    printf("Sine hyperbolic of %.2lf (in radians) = %.2lf\n", arg, result);
    
    arg=4000;
    result = sinh(arg);
    printf("Sine hyperbolic of %.2lf (in radians) = %.2lf", arg, result);
    return 0;
}

Output 8

Sine hyperbolic of -6.78 radians = -440.03
Sine hyperbolic of 4000.00 radians = inf  

cosh(), coshf(), coshl()

The cosh() function returns the hyperbolic cosine of an argument (argument in radians).

double cosh( double arg );

If the argument has type int or the type double, cosh is called.

float coshf( float arg );

If the argument has type float , coshf is called.

(Since C99)

long double coshl( long double arg );

If the argument has integer type long double , coshl is called.

(Since C99)

Return value

The return value of cosh() lies between +∞ and -∞

  • If the argument is ±0, 1 is returned
  • If the argument is ±∞, +∞ is returned
  • If the argument is NaN, NaN is returned

Example 9

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

int main ()
{
    double arg;
    double result;

    arg = -0.2;
    result = cosh(arg);
    printf("Hyperbolic cosine of %lf radians = %lf\n", arg, result);

    arg = 0;
    result = cosh(arg);
    printf("Hyperbolic cosine of %lf in radians = %lf\n", arg, result);

    arg = 1.5;
    result = cosh(arg);
    printf("Hyperbolic cosine of %lf in radians = %lf\n", arg, result);

    return 0;
}

Output 9

Hyperbolic cosine of -0.200000 radians = 1.020067   
Hyperbolic cosine of 0.000000 radians = 1.000000 
Hyperbolic cosine of 1.500000 radians = 2.352410 

tanh(), tanhf(), tanhl()

The tanh() function returns the hyperbolic tangent of an argument (argument in radians).

double tanh( double arg );

If the argument has type int or the type double, tanh is called.

float tanhf( float arg );

If the argument has type float , tanhf is called.

(Since C99)

long double tanhl( long double arg );

If the argument has type long double , tanhl is called.

(Since C99)

Return value

  • If the argument is ±0, 1 is returned
  • If the argument is ±∞, +∞ is returned
  • If the argument is NaN, NaN is returned

Example 10

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

int main()
{
    double arg;
    double result;
    
    arg=0.5;
    result = tanh(arg);
    printf("Tangent hyperbolic of %.2lf radians = %.2lf\n", arg, result);
    
    arg=6000;
    result = tanh(arg);
    printf("Tangent hyperbolic of %.2lf radians = %.2lf", arg, result);
    return 0;
}

Output 10

Tangent hyperbolic of 0.50 radians = 0.46      
Tangent hyperbolic of 6000.00 radians = 1.00

asinh(), asinhf(), asinhl()

The asinh() function returns the inverse hyperbolic sine of a number

Although the C standard names this function as "arc hyperbolic sine",the correct name is "inverse hyperbolic sine" or "area hyperbolic sine" as The argument is the area of a hyperbolic sector, not an arc..

double asinh( double arg );

If the argument has type int or the type double, asinh is called.

float asinhf( float arg );

If the argument has type float , asinhf is called.

(Since C99)

long double asinhl( long double arg );

If the argument has type long double , asinhl is called.

(Since C99)

Return value

The return value of asinh() lies between +∞ and -∞

  • If the argument is ±0 or ±∞, it is returned unmodified
  • if the argument is NaN, NaN is returned

Example 11

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

int main()
{
	double arg;
	double result;
	
	arg=0.5;
	result = asinh(arg);
	printf("Hyperbolic Inverse of %.2f = %.2f radians\n",arg, result);
	
	arg=1;
	result = asinh(arg);
	printf("Hyperbolic Inverse of %.2f = %.2f radians",arg, result);
	return 0;
}

Output 11

Hyperbolic Inverse of 0.50 = 0.48 radians   
Hyperbolic Inverse of 1.00 = 0.88 radians    

acosh(), acoshf(), acoshl()

The acosh() function returns the inverse hyperbolic cosine of an argument in radians.

double acosh( double arg );

If the argument has type int or the type double, acosh is called.

float acoshf( float arg );

If the argument has type float , acoshf is called.

(Since C99)

long double acoshl( long double arg );

If the argument has type long double , acoshl is called.

(Since C99)

Parameter

The acosh() function takes a single argument where argument should be equal to or greater than 1 (arg ≥ 1)

Return value

The return value of acosh() lies between 0 and +∞ in radians
RANGE: [0, +∞]

  • if the argument is less than 1, NaN is returned
  • if the argument is 1, +0 is returned
  • if the argument is +∞, +∞ is returned
  • if the argument is NaN, NaN is returned

Example 12

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

int main()
{
    double arg;
    double result;

    arg = 8.9;
    result = acosh(arg);
    printf("acosh(%.2f) = %.2lf radians\n", arg, result);

    // parameter not in range
    arg = 0.3;
    result = acosh(arg);
    printf("acosh(%.2f) = %.2lf", arg, result);

    return 0;
}

Output 12

acosh(8.90) = 2.88 radians                      
acosh(0.30) = -nan  

atanh(), atanhf(), atanhl()

The atanh() function returns the inverse hyperbolic tangent of an argument in radians.

double atanh( double arg );

If the argument has type int or the type double, atanh is called.

float atanhf( float arg );

If the argument has type float , atanhf is called.

(Since C99)

long double atanl( long double arg );

If the argument has type long double , atanl is called.

(Since C99)

Parameter

The atanh() function takes a single argument between (-1 ≤ arg ≥ 1)

Return value

  • if the argument is ±1, ±∞ is returned
  • if arg>1 or arg<1, NaN is returned
  • if the argument is NaN, NaN is returned

Example 13

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

int main()
{
    double arg;
    double result;

    arg =  -0.5;
    result = atanh(arg);
    printf("atanh(%.2f) = %.2lf radians\n", arg, result);

    // parameter not in range
    arg = 1.01;
    result = atanh(arg);
    printf("atanh(%.2f) = %.2lf", arg, result);

    return 0;
}

Output 13

atanh(-0.50) = -0.55 radians     
atanh(1.01) = -nan  

All the above trigonometric fuctions take into account real numbers...so what about complex numbers???

The header <tgmath.h> includes the headers <math.h> and <complex.h>. It defines several trigonometric functions that can determine real or complex functions to be called based on the types of the arguments. (Since C99)

This article at OpenGenus completes the list of all trigonometric functions predefined in the <math.h> header in C. You can incorporate them in your C program to calculate any trigonometric function.

Trigonometric Functions using <math.h> in C
Share this