Namespaces
Variants

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

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float cos ( float num ) ;

double cos ( double num ) ;

long double cos ( long double num ) ;
(C++23 前)
/*floating-point-type*/
cos ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float cosf ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double cosl ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

cos ( const V & v_num ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double cos ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算以弧度计量的 num 的余弦值。 库为所有 cv 未限定浮点类型提供了 std::cos 的重载版本作为参数类型。 (C++23 起)
S) SIMD重载对 v_num 执行逐元素的 std::cos 运算。
(定义参见 math-floating-point deduced-simd-t
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型被视为 double
(since C++11)

目录

参数

num - 表示角度(以弧度为单位)的浮点数或整数值

返回值

如果未发生错误,则返回 num 的余弦值 cos(num) ,其取值范围为 [ - 1.0 , + 1.0 ]

num 的幅值较大,则结果可能几乎没有意义或毫无意义。

(until C++11)

如果发生定义域错误,则返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为 ±0,结果为 1.0
  • 若参数为 ±∞,将返回 NaN 并触发 FE_INVALID
  • 若参数为 NaN,将返回 NaN。

注释

当参数为无穷大的情况在 C 语言中未被明确定义为域错误,但根据 POSIX 标准 被定义为域错误。

额外的重载并不要求完全按照 (A) 的形式提供。它们只需确保对于整数类型的实参 num std :: cos ( num ) 具有与 std :: cos ( static_cast < double > ( num ) ) 相同的效果。

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numbers>
// #pragma STDC FENV_ACCESS ON
constexpr double pi = std::numbers::pi; // 或 C++20 之前的 std::acos(-1)
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
              << "典型用法:\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'
              << "特殊值:\n"
              << "std::cos(+0) = " << std::cos(0.0) << '\n'
              << "std::cos(-0) = " << std::cos(-0.0) << '\n';
    // 错误处理
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "cos(INFINITY) = " << std::cos(INFINITY) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    触发了 FE_INVALID 异常\n";
}

可能的输出:

典型用法:
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
特殊值:
std::cos(+0) = +1
std::cos(-0) = +1
cos(INFINITY) = -nan
    触发了 FE_INVALID 异常

参阅

(C++11) (C++11)
计算正弦值 ( sin(x) )
(函数)
(C++11) (C++11)
计算正切值 ( tan(x) )
(函数)
(C++11) (C++11)
计算反余弦值 ( arccos(x) )
(函数)
计算复数的余弦值 ( cos(z) )
(函数模板)
对 valarray 的每个元素应用函数 std::cos
(函数模板)
C 文档 关于 cos