Namespaces
Variants
Views
Actions

tan

From cppreference.com
< c‎ | numeric‎ | math
 
 
 
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
Exponential functions
(C99)
(C99)
(C99)
(C99)
Power functions
(C99)
(C99)
Trigonometric and hyperbolic functions
(C99)
(C99)
(C99)
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Nearest integer floating point operations
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
Floating point manipulation functions
(C99)(C99)
(C99)
(C99)
Classification
(C99)
(C99)
(C99)
Macro constants
 
Defined in header <math.h>
float       tanf( float arg );
(since C99)
double      tan( double arg );
long double tanl( long double arg );
(since C99)

Computes tangent of arg

Contents

[edit] Parameters

arg - floating point value representing angle in radians

[edit] Return value

Tangent of arg.

Domain error occurs if arg is infinite. NAN is returned in that case.

[edit] Example

#include <stdio.h>
#include <math.h>
#include <errno.h>
#include <fenv.h>
#include <string.h>
#define PI acos(-1)
 
#pragma STDC FENV_ACCESS ON
 
void show_fe_exceptions(void)
{
    printf("current exceptions raised: ");
    if(fetestexcept(FE_DIVBYZERO))     printf(" FE_DIVBYZERO");
    if(fetestexcept(FE_INEXACT))       printf(" FE_INEXACT");
    if(fetestexcept(FE_INVALID))       printf(" FE_INVALID");
    if(fetestexcept(FE_OVERFLOW))      printf(" FE_OVERFLOW");
    if(fetestexcept(FE_UNDERFLOW))     printf(" FE_UNDERFLOW");
    if(fetestexcept(FE_ALL_EXCEPT)==0) printf(" none");
    printf("\n");
}
 
int main(void)
{                                                           /* Angle  Quad */
                                                            /* -----  ---- */
    printf("tan(PI*1.0/4.0) = %+.1f\n", tan(PI*1.0/4.0));   /*   +45     I */
    printf("tan(PI*3.0/4.0) = %+.1f\n", tan(PI*3.0/4.0));   /*  +135    II */
    printf("tan(PI*5.0/4.0) = %+.1f\n", tan(PI*5.0/4.0));   /*  -135   III */
    printf("tan(PI*7.0/4.0) = %+.1f\n", tan(PI*7.0/4.0));   /*   -45    IV */
    printf("\n");
 
    printf("tan(+0.0) = %+.1f\n", tan(+0.0));
    printf("tan(-0.0) = %+.1f\n", tan(-0.0));
    printf("tan(NAN)  = %.1f\n",  tan(NAN));
    printf("\n");
 
    /* domain error                       */
    /* "invalid" floating-point exception */
    /* same with tan(-INFINITY)           */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("tan(INFINITY) = %f\n", tan(INFINITY));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

tan(PI*1.0/4.0) = +1.0
tan(PI*3.0/4.0) = -1.0
tan(PI*5.0/4.0) = +1.0
tan(PI*7.0/4.0) = -1.0
 
tan(+0.0) = +0.0
tan(-0.0) = -0.0
tan(NAN)  = nan
 
tan(INFINITY) = -nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID

[edit] See also

computes sine (sin(x))
(function) [edit]
computes cosine (cos(x))
(function) [edit]
computes arc tangent (arctan(x))
(function) [edit]