名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <cmath>
(1)
float       sin ( float num );

double      sin ( double num );

long double sin ( long double num );
(直至 C++23)
/*浮點型別*/
            sin ( /*浮點型別*/ 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< /*數學浮點型別*/ V >

constexpr /*推導 SIMD 型別*/<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-unqualified 浮點型別提供了 std::sin 的過載作為引數型別。(C++23 起)
S) SIMD 過載對 v_num 執行逐元素 std::sin
(有關它們的定義,請參閱 math-floating-pointdeduced-simd-t。)
(C++26 起)
A) 為所有整數型別提供了額外的過載,它們被視為 double
(C++11 起)

目錄

[編輯] 引數

num - 表示弧度角的浮點數或整數值

[編輯] 返回值

如果沒有錯誤發生,則返回 num 的正弦值 (sin(num)),範圍為 [-1+1]

如果 num 的幅度很大,結果可能沒有或幾乎沒有意義。

(C++11 前)

如果發生域錯誤,則返回實現定義的值 (支援 NaN 時返回 NaN)。

如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。

[編輯] 錯誤處理

錯誤按 math_errhandling 指定的方式報告。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • 如果引數為 ±0,則原樣返回。
  • 如果引數為 ±∞,則返回 NaN 並引發 FE_INVALID
  • 如果引數為 NaN,則返回 NaN。

[編輯] 注意

C 語言(C++ 遵循)未將引數為無窮大的情況指定為域錯誤,但在 POSIX 中定義為域錯誤

POSIX 還規定,在下溢的情況下,num 原樣返回;如果不支援,則返回一個不大於 DBL_MINFLT_MINLDBL_MIN 的實現定義值。

額外的過載不要求完全按照 (A) 提供。它們只需要足以確保對於整數型別的引數 numstd::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); // or std::numbers::pi since C++20
 
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
              << "Typical usage:\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'
              << "Special values:\n"
              << "std::sin(+0) = " << std::sin(0.0) << '\n'
              << "std::sin(-0) = " << std::sin(-0.0) << '\n';
 
    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "std::sin(INFINITY) = " << std::sin(INFINITY) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

可能的輸出

Typical usage:
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
Special values:
std::sin(+0) = +0
std::sin(-0) = -0
std::sin(INFINITY) = -nan
    FE_INVALID raised

[編輯] 參閱

(C++11)(C++11)
計算餘弦(cos(x)
(函式) [編輯]
(C++11)(C++11)
計算正切(tan(x)
(函式) [編輯]
(C++11)(C++11)
計算反正弦(arcsin(x)
(函式) [編輯]
計算複數的正弦 (sin(z))
(函式模板) [編輯]
將函式 std::sin 應用到 valarray 的每個元素
(函式模板) [編輯]
C 文件 用於 sin