std::cos,std::cosf,std::cosl (3) - Linux Manuals

std::cos,std::cosf,std::cosl: std::cos,std::cosf,std::cosl

NAME

std::cos,std::cosf,std::cosl - std::cos,std::cosf,std::cosl

Synopsis


Defined in header <cmath>
float cos ( float arg );
float cosf( float arg ); (since C++11)
double cos ( double arg ); (1) (2)
long double cos ( long double arg );
long double cosl( long double arg ); (3) (since C++11)
double cos ( IntegralType arg ); (4) (since C++11)


1-3) Computes the cosine of arg (measured in radians).
4) A set of overloads or a function template accepting an argument of any integral_type. Equivalent to 2) (the argument is cast to double).

Parameters


arg - value representing angle in radians, of a floating-point or Integral_type

Return value


If no errors occur, the cosine of arg (cos(arg)) in the range [-1 ; +1], is returned.


The result may have little or no significance if the magnitude of arg is large (until C++11)


If a domain error occurs, an implementation-defined value is returned (NaN where supported)
If a range error occurs due to underflow, the correct result (after rounding) is returned.

Error handling


Errors are reported as specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),


* if the argument is ±0, the result is 1.0
* if the argument is ±∞, NaN is returned and FE_INVALID is raised
* if the argument is NaN, NaN is returned

Notes


The case where the argument is infinite is not specified to be a domain error in C, but it is defined as a domain_error_in_POSIX

Example


// Run this code


  #include <iostream>
  #include <cmath>
  #include <cerrno>
  #include <cfenv>


  #pragma STDC FENV_ACCESS ON
  const double pi = std::acos(-1);
  int main()
  {
      // typical usage
      std::cout << "cos(pi/3) = " << std::cos(pi/3) << '\n'
                << "cos(pi/2) = " << std::cos(pi/2) << '\n'
                << "cos(-3*pi/4) = " << std::cos(-3*pi/4) << '\n';
      // special values
      std::cout << "cos(+0) = " << std::cos(0.0) << '\n'
                << "cos(-0) = " << std::cos(-0.0) << '\n';
      // error handling
      std::feclearexcept(FE_ALL_EXCEPT);
      std::cout << "cos(INFINITY) = " << std::cos(INFINITY) << '\n';
      if (std::fetestexcept(FE_INVALID))
          std::cout << " FE_INVALID raised\n";
  }

Possible output:


  cos(pi/3) = 0.5
  cos(pi/2) = 6.12323e-17
  cos(-3*pi/4) = -0.707107
  cos(+0) = 1
  cos(-0) = 1
  cos(INFINITY) = -nan
      FE_INVALID raised

See also


sin
sinf
sinl computes sine (sin(x))
                   (function)


(C++11)
(C++11)


tan
tanf
tanl computes tangent (tan(x))
                   (function)


(C++11)
(C++11)


acos
acosf
acosl computes arc cosine (arccos(x))
                   (function)


(C++11)
(C++11)
                   computes cosine of a complex number (cos(z))
cos(std::complex) (function template)
                   applies the function std::cos to each element of valarray
cos(std::valarray) (function template)