Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/cos"

From cppreference.com
< cpp‎ | numeric‎ | math
m (Space removed: range [-1 ; +1] -> range [-1, +1])
m (Return value: {{closed range}})
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{cpp/title|cos|cosf|cosl}}
 
{{cpp/title|cos|cosf|cosl}}
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
{{dcl begin}}
+
{{cpp/numeric/math/declarations
{{dcl header | cmath}}
+
|family=cos
{{dcl rev multi| num=1
+
|param1=num
|until1=|dcl1=
+
|constexpr_since=26
float      cos ( float arg );
+
|desc=Computes the cosine of {{c|num}} (measured in radians).
|since2=c++11|dcl2=
+
float      cosf( float arg );
+
 
}}
 
}}
{{dcl |num=2|
 
double      cos ( double arg );
 
}}
 
{{dcl rev multi| num=3
 
|until1=|dcl1=
 
long double cos ( long double arg );
 
|since2=c++11|dcl2=
 
long double cosl( long double arg );
 
}}
 
{{dcl | num=4| since=c++11 |
 
double      cos ( IntegralType arg );
 
}}
 
{{dcl end}}
 
 
@1-3@ Computes the cosine of {{tt|arg}} (measured in radians).
 
@4@ A set of overloads or a function template accepting an argument of any [[cpp/types/is_integral|integral type]]. Equivalent to 2) (the argument is cast to {{c|double}}).
 
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | arg | value representing angle in radians, of a floating-point or [[cpp/types/is_integral|Integral type]]}}
+
{{par|num|floating-point or integer value representing angle in radians}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
If no errors occur, the cosine of {{tt|arg}} ({{math|cos(arg)}}) in the range {{math|[-1, +1]}}, is returned.
+
If no errors occur, the cosine of {{c|num}} ({{math|cos(num)}}) in the range {{closed range|-1.0|+1.0}}, is returned.
  
 
{{rrev|until=c++11|
 
{{rrev|until=c++11|
The result may have little or no significance if the magnitude of {{tt|arg}} is large
+
The result may have little or no significance if the magnitude of {{c|num}} is large.
 
}}
 
}}
  
If a domain error occurs, an implementation-defined value is returned (NaN where supported)
+
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.
 
If a range error occurs due to underflow, the correct result (after rounding) is returned.
Line 46: Line 28:
  
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
* if the argument is ±0, the result is 1.0
+
* if the argument is ±0, the result is {{c|1.0}}.
* if the argument is ±∞, NaN is returned and {{lc|FE_INVALID}} is raised
+
* if the argument is ±∞, NaN is returned and {{lc|FE_INVALID}} is raised.
* if the argument is NaN, NaN is returned
+
* if the argument is NaN, NaN is returned.
  
 
===Notes===
 
===Notes===
The case where the argument is infinite is not specified to be a domain error in C, but it is defined as a [http://pubs.opengroup.org/onlinepubs/9699919799/functions/cos.html domain error in POSIX]
+
The case where the argument is infinite is not specified to be a domain error in C, but it is defined as a [https://pubs.opengroup.org/onlinepubs/9699919799/functions/cos.html domain error in POSIX].
 +
 
 +
{{cpp/numeric/math/additional integer overload note|cos}}
  
 
===Example===
 
===Example===
{{example|code=
+
{{example
 +
|code=
 
#include <cerrno>
 
#include <cerrno>
 
#include <cfenv>
 
#include <cfenv>
 
#include <cmath>
 
#include <cmath>
 +
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
 +
#include <numbers>
  
#pragma STDC FENV_ACCESS ON
+
// #pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1);
+
 
 +
constexpr double pi = std::numbers::pi; // or std::acos(-1) before C++20
 +
 
 +
constexpr double your_cos(double x)
 +
{
 +
    double cos{1}, pow{x};
 +
    for (auto fac{1ull}, n{1ull}; n != 19; fac *= ++n, pow *= x)
 +
        if ((n & 1) == 0)
 +
            cos += (n & 2 ? -pow : pow) / fac;
 +
    return cos;
 +
}
  
 
int main()
 
int main()
 
{
 
{
    // typical usage
+
     std::cout << std::setprecision(10) << std::showpos
     std::cout << "cos(pi/3) = " << std::cos(pi/3) << '\n'
+
              << "Typical usage:\n"
               << "cos(pi/2) = " << std::cos(pi/2) << '\n'
+
              << "std::cos(pi/3) = " << std::cos(pi / 3) << '\n'
               << "cos(-3*pi/4) = " << std::cos(-3*pi/4) << '\n';
+
               << "your cos(pi/3) = " << your_cos(pi / 3) << '\n'
    // special values
+
              << "std::cos(pi/2) = " << std::cos(pi / 2) << '\n'
    std::cout << "cos(+0) = " << std::cos(0.0) << '\n'
+
               << "your cos(pi/2) = " << your_cos(pi / 2) << '\n'
               << "cos(-0) = " << std::cos(-0.0) << '\n';
+
              << "std::cos(-3*pi/4) = " << std::cos(-3 * pi / 4) << '\n'
 +
              << "your cos(-3*pi/4) = " << your_cos(-3 * pi / 4) << '\n'
 +
              << "Special values:\n"
 +
              << "std::cos(+0) = " << std::cos(0.0) << '\n'
 +
               << "std::cos(-0) = " << std::cos(-0.0) << '\n';
 +
 
 
     // error handling
 
     // error handling
 
     std::feclearexcept(FE_ALL_EXCEPT);
 
     std::feclearexcept(FE_ALL_EXCEPT);
 +
 
     std::cout << "cos(INFINITY) = " << std::cos(INFINITY) << '\n';
 
     std::cout << "cos(INFINITY) = " << std::cos(INFINITY) << '\n';
 
     if (std::fetestexcept(FE_INVALID))
 
     if (std::fetestexcept(FE_INVALID))
 
         std::cout << "    FE_INVALID raised\n";
 
         std::cout << "    FE_INVALID raised\n";
 
}
 
}
| p=true
+
|p=true
| output=
+
|output=
cos(pi/3) = 0.5
+
Typical usage:
cos(pi/2) = 6.12323e-17
+
std::cos(pi/3) = +0.5
cos(-3*pi/4) = -0.707107
+
your cos(pi/3) = +0.5
cos(+0) = 1
+
std::cos(pi/2) = +6.123233996e-17
cos(-0) = 1
+
your cos(pi/2) = -3.373452105e-15
 +
std::cos(-3*pi/4) = -0.7071067812
 +
your cos(-3*pi/4) = -0.7071067812
 +
Special values:
 +
std::cos(+0) = +1
 +
std::cos(-0) = +1
 
cos(INFINITY) = -nan
 
cos(INFINITY) = -nan
 
     FE_INVALID raised
 
     FE_INVALID raised
Line 91: Line 99:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/math/dsc sin}}
+
{{dsc inc|cpp/numeric/math/dsc sin}}
{{dsc inc | cpp/numeric/math/dsc tan}}
+
{{dsc inc|cpp/numeric/math/dsc tan}}
{{dsc inc | cpp/numeric/math/dsc acos}}
+
{{dsc inc|cpp/numeric/math/dsc acos}}
{{dsc inc | cpp/numeric/complex/dsc cos}}
+
{{dsc inc|cpp/numeric/complex/dsc cos}}
{{dsc inc | cpp/numeric/valarray/dsc cos}}
+
{{dsc inc|cpp/numeric/valarray/dsc cos}}
{{dsc see c | c/numeric/math/cos}}
+
{{dsc see c|c/numeric/math/cos}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pl|pt|ru|zh}}

Latest revision as of 08:47, 13 November 2023

 
 
 
 
Defined in header <cmath>
(1)
float       cos ( float num );

double      cos ( double num );

long double cos ( long double num );
(until C++23)
/* floating-point-type */
            cos ( /* floating-point-type */ num );
(since C++23)
(constexpr since C++26)
float       cosf( float num );
(2) (since C++11)
(constexpr since C++26)
long double cosl( long double num );
(3) (since C++11)
(constexpr since C++26)
Additional overloads (since C++11)
Defined in header <cmath>
template< class Integer >
double      cos ( Integer num );
(A) (constexpr since C++26)
1-3) Computes the cosine of num (measured in radians). The library provides overloads of std::cos for all cv-unqualified floating-point types as the type of the parameter.(since C++23)
A) Additional overloads are provided for all integer types, which are treated as double.
(since C++11)

Contents

[edit] Parameters

num - floating-point or integer value representing angle in radians

[edit] Return value

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

The result may have little or no significance if the magnitude of num 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.

[edit] 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.

[edit] 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.

The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::cos(num) has the same effect as std::cos(static_cast<double>(num)).

[edit] Example

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>
 
// #pragma STDC FENV_ACCESS ON
 
constexpr double pi = std::numbers::pi; // or std::acos(-1) before C++20
 
constexpr double your_cos(double x)
{
    double cos{1}, pow{x};
    for (auto fac{1ull}, n{1ull}; n != 19; fac *= ++n, pow *= x)
        if ((n & 1) == 0)
            cos += (n & 2 ? -pow : pow) / fac;
    return cos;
}
 
int main()
{
    std::cout << std::setprecision(10) << std::showpos
              << "Typical usage:\n"
              << "std::cos(pi/3) = " << std::cos(pi / 3) << '\n'
              << "your cos(pi/3) = " << your_cos(pi / 3) << '\n'
              << "std::cos(pi/2) = " << std::cos(pi / 2) << '\n'
              << "your cos(pi/2) = " << your_cos(pi / 2) << '\n'
              << "std::cos(-3*pi/4) = " << std::cos(-3 * pi / 4) << '\n'
              << "your cos(-3*pi/4) = " << your_cos(-3 * pi / 4) << '\n'
              << "Special values:\n"
              << "std::cos(+0) = " << std::cos(0.0) << '\n'
              << "std::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:

Typical usage:
std::cos(pi/3) = +0.5
your cos(pi/3) = +0.5
std::cos(pi/2) = +6.123233996e-17
your cos(pi/2) = -3.373452105e-15
std::cos(-3*pi/4) = -0.7071067812
your cos(-3*pi/4) = -0.7071067812
Special values:
std::cos(+0) = +1
std::cos(-0) = +1
cos(INFINITY) = -nan
    FE_INVALID raised

[edit] See also

(C++11)(C++11)
computes sine (sin(x))
(function) [edit]
(C++11)(C++11)
computes tangent (tan(x))
(function) [edit]
(C++11)(C++11)
computes arc cosine (arccos(x))
(function) [edit]
computes cosine of a complex number (cos(z))
(function template) [edit]
applies the function std::cos to each element of valarray
(function template) [edit]