Namespaces
Variants
Views
Actions

sin

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       sinf( float arg );
(since C99)
double      sin( double arg );
long double sinl( long double arg );
(since C99)

Computes sine of arg

Contents

[edit] Parameters

arg - floating point value representing angle in radians

[edit] Return value

Sine of arg in the range [-1 ; +1].

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)
{
    printf("sin(PI/2.0)   = %.1f\n", sin(PI/2.0));
    printf("sin(+0.0)     = %.1f\n", sin(+0.0));
    printf("sin(-0.0)     = %.1f\n", sin(-0.0));
    printf("sin(NAN)      = %.1f\n", sin(NAN));
    printf("\n");
 
    /* domain error                       */
    /* "invalid" floating-point exception */
    /* same with sin(-INFINITY)           */
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("sin(INFINITY) = %f\n", sin(INFINITY));
    printf("%s\n",strerror(errno));
    show_fe_exceptions();
 
    return 0;
}

Possible output:

sin(PI/2.0)   = 1.0
sin(+0.0)     = 0.0
sin(-0.0)     = -0.0
sin(NAN)      = nan
 
sin(INFINITY) = -nan
Numerical argument out of domain
current exceptions raised:  FE_INVALID

[edit] See also

computes cosine (cos(x))
(function) [edit]
computes tangent (tan(x))
(function) [edit]
computes arc sine (arcsin(x))
(function) [edit]