tan, tanf, tanl

< c‎ | numeric‎ | math
在标头 <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)。

仅当实现预定义了 __STDC_IEC_60559_DFP__(即实现支持十进制浮点数)时,声明函数 (4-6)

(C23 起)

参数

arg - 以弧度表示角的浮点值

返回值

若不出现错误,则返回 arg 的正切(tan(arg))。

arg 的绝对值很大,则结果可能有较少或无有效数字。

(C99 前)

若出现定义域错误,则返回实现定义值(受支持平台为 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);
 
    // 典型用法
    printf("tan(pi*1/4) = %+f\n", tan(pi * 1 / 4)); //   45°
    printf("tan(pi*3/4) = %+f\n", tan(pi * 3 / 4)); //  135°
    printf("tan(pi*5/4) = %+f\n", tan(pi * 5 / 4)); // -135°
    printf("tan(pi*7/4) = %+f\n", tan(pi * 7 / 4)); //  -45°
 
    // 特殊值
    printf("tan(+0) = %f\n", tan(0.0));
    printf("tan(-0) = %f\n", tan(-0.0));
 
    // 错误处理
    feclearexcept(FE_ALL_EXCEPT);
    printf("tan(INFINITY) = %f\n", tan(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

可能的输出:

tan  (pi/4) = +1.000000
tan(3*pi/4) = -1.000000
tan(5*pi/4) = +1.000000
tan(7*pi/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 The tan functions (第 TBD 页)
  • 7.25 Type-generic math <tgmath.h> (第 TBD 页)
  • F.10.1.7 The tan functions (第 TBD 页)
  • C17 标准(ISO/IEC 9899:2018):
  • 7.12.4.7 The tan functions (第 175 页)
  • 7.25 Type-generic math <tgmath.h> (第 272-273 页)
  • F.10.1.7 The tan functions (第 378 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.12.4.7 The tan functions (第 240 页)
  • 7.25 Type-generic math <tgmath.h> (第 373-375 页)
  • F.10.1.7 The tan functions (第 519 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.12.4.7 The tan functions (第 220 页)
  • 7.22 Type-generic math <tgmath.h> (第 335-337 页)
  • F.9.1.7 The tan functions (第 457 页)
  • C89/C90 标准(ISO/IEC 9899:1990):
  • 4.5.2.7 The tan function

参阅

(C99)(C99)
计算正弦(sin(x)
(函数)
(C99)(C99)
计算余弦(cos(x)
(函数)
(C99)(C99)
计算反正切(arctan(x)
(函数)
(C99)(C99)(C99)
计算复数正切
(函数)