名稱空間
變體
操作

tan, tanf, tanl

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指數函式
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
冪函式
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函式和雙曲函式
tan
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整數浮點數
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮點數操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(C99)
(C99)
(C99)
(C23)
誤差函式和伽馬函式
(C99)
(C99)
(C99)
(C99)
型別
宏常量
特殊浮點值
(C99)(C23)
引數和返回值
錯誤處理
快速操作指示符
 
定義於標頭檔案 <math.h>
float       tanf( float arg );
(1) (C99 起)
double      tan( double arg );
(2)
long double tanl( long double arg );
(3) (C99 起)
_Decimal32  tand32( _Decimal32 arg );
(4) (自 C23 起)
_Decimal64  tand64( _Decimal64 arg );
(5) (自 C23 起)
_Decimal128 tand128( _Decimal128 arg );
(6) (自 C23 起)
定義於標頭檔案 <tgmath.h>
#define tan( arg )
(7) (C99 起)
1-6) 計算 arg(以弧度表示)的正切值。
7) 泛型宏:如果引數型別為 long double,則呼叫 (3) (tanl)。否則,如果引數型別為整數型別或 double,則呼叫 (2) (tan)。否則,呼叫 (1) (tanf)。如果引數是複數,則該宏呼叫相應的複數函式(ctanfctanctanl)。

函式 (4-6) 僅當實現預定義 __STDC_IEC_60559_DFP__(即實現支援十進位制浮點數)時才宣告。

(自 C23 起)

目錄

[編輯] 引數

arg - 表示弧度角的浮點值

[編輯] 返回值

如果沒有錯誤發生,則返回 arg 的正切值 (tan(arg))。

如果 arg 的量級較大,結果可能沒有或只有很少的意義。

(直到 C99)

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

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

[編輯] 錯誤處理

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

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

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

[編輯] 注意

C 標準中沒有將引數為無窮大的情況指定為域錯誤,但在 POSIX 中定義為域錯誤

該函式在 π(1/2 + n) 處有數學極點;但是,沒有常見的浮點表示能夠精確表示 π/2,因此沒有引數值會導致極點錯誤。

[編輯] 示例

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
 
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
 
int main(void)
{
    const double pi = acos(-1);
 
    // typical usage
    printf("tan(pi*1/4) = %+f\n", tan(pi * 1 / 4)); //   45 deg
    printf("tan(pi*3/4) = %+f\n", tan(pi * 3 / 4)); //  135 deg
    printf("tan(pi*5/4) = %+f\n", tan(pi * 5 / 4)); // -135 deg
    printf("tan(pi*7/4) = %+f\n", tan(pi * 7 / 4)); //  -45 deg
 
    // special values
    printf("tan(+0) = %f\n", tan(0.0));
    printf("tan(-0) = %f\n", tan(-0.0));
 
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("tan(INFINITY) = %f\n", tan(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

可能的輸出

tan(pi*1/4) = +1.000000
tan(pi*3/4) = -1.000000
tan(pi*5/4) = +1.000000
tan(pi*7/4) = -1.000000
tan(+0) = 0.000000
tan(-0) = -0.000000
tan(INFINITY) = -nan
    FE_INVALID raised

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.12.4.7 tan 函式 (p: TBD)
  • 7.25 型別通用數學 <tgmath.h> (p: TBD)
  • F.10.1.7 tan 函式 (p: TBD)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.12.4.7 tan 函式 (p: 175)
  • 7.25 型別通用數學 <tgmath.h> (p: 272-273)
  • F.10.1.7 tan 函式 (p: 378)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.12.4.7 tan 函式 (p: 240)
  • 7.25 型別通用數學 <tgmath.h> (p: 373-375)
  • F.10.1.7 tan 函式 (p: 519)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.12.4.7 tan 函式 (p: 220)
  • 7.22 型別通用數學 <tgmath.h> (p: 335-337)
  • F.9.1.7 tan 函式 (p: 457)
  • C89/C90 標準 (ISO/IEC 9899:1990)
  • 4.5.2.7 tan 函式

[編輯] 另請參閱

(C99)(C99)
計算正弦 (sin(x))
(函式) [編輯]
(C99)(C99)
計算餘弦 (cos(x))
(函式) [編輯]
(C99)(C99)
計算反正切 (arctan(x))
(函式) [編輯]
(C99)(C99)(C99)
計算復正切
(函式) [編輯]
C++ 文件 關於 tan