Namespaces
Variants

std:: sin, std:: sinf, std:: sinl

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 sin ( float num ) ;

double sin ( double num ) ;

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

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

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

目录

参数

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

返回值

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

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

(until C++11)

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

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

错误处理

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

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

  • 如果参数为 ±0,则原样返回。
  • 如果参数为 ±∞,则返回 NaN 并引发 FE_INVALID
  • 如果参数为 NaN,则返回 NaN。

注释

当参数为无穷大的情况在 C 语言(C++ 遵循其规范)中未被指定为定义域错误,但根据 POSIX 标准 被定义为定义域错误。

POSIX 还规定,在下溢情况下, num 将保持原样返回;若不支持此行为,则返回一个不大于 DBL_MIN FLT_MIN LDBL_MIN 的实现定义值。

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

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <iomanip>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1); // 或 C++20 起的 std::numbers::pi
constexpr double your_sin(double x)
{
    double sin{0}, pow{x};
    for (auto fac{1LLU}, n{1ULL}; n != 20; fac *= ++n, pow *= x)
        if (n & 1)
            sin += (n & 2 ? -pow : pow) / fac;
    return sin;
}
int main()
{
    std::cout << std::setprecision(10) << std::showpos
              << "典型用法:\n"
              << "std::sin(pi/6) = " << std::sin(pi / 6) << '\n'
              << "your sin(pi/6) = " << your_sin(pi / 6) << '\n'
              << "std::sin(pi/2) = " << std::sin(pi / 2) << '\n'
              << "your sin(pi/2) = " << your_sin(pi / 2) << '\n'
              << "std::sin(-3*pi/4) = " << std::sin(-3 * pi / 4) << '\n'
              << "your sin(-3*pi/4) = " << your_sin(-3 * pi / 4) << '\n'
              << "特殊值:\n"
              << "std::sin(+0) = " << std::sin(0.0) << '\n'
              << "std::sin(-0) = " << std::sin(-0.0) << '\n';
    // 错误处理
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "std::sin(INFINITY) = " << std::sin(INFINITY) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    触发了 FE_INVALID\n";
}

可能的输出:

典型用法:
std::sin(pi/6) = +0.5
your sin(pi/6) = +0.5
std::sin(pi/2) = +1
your sin(pi/2) = +1
std::sin(-3*pi/4) = -0.7071067812
your sin(-3*pi/4) = -0.7071067812
特殊值:
std::sin(+0) = +0
std::sin(-0) = -0
std::sin(INFINITY) = -nan
    触发了 FE_INVALID

参见

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