tan, tanf, tanl
来自 cppreference.cn
定义于头文件 <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
)。若实参为复数,则此宏调用相应的复数函数(ctanf、ctan、ctanl)。
函数 (4-6) 声明为存在当且仅当实现预定义了 |
(自 C23 起) |
目录 |
[编辑] 参数
arg | - | 浮点值,表示以弧度为单位的角度 |
[编辑] 返回值
若未发生错误,则返回 arg 的正切值 (tan(arg))。
若 arg 的幅度过大,则结果可能几乎或完全无意义。 |
(直到 C99) |
若发生域错误,则返回实现定义值(若支持则为 NaN)。
若因下溢发生值域错误,则返回正确结果(舍入后)。
[编辑] 错误处理
错误如 math_errhandling
中指定报告。
若实现支持 IEEE 浮点算术 (IEC 60559)
- 若参数为 ±0,则原样返回;
- 若参数为 ±∞,则返回 NaN 并引发 FE_INVALID;
- 若参数为 NaN,则返回 NaN。
[编辑] 注解
C 标准中未指定参数为无穷的情况是域错误,但 POSIX 中定义为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 函数 (页码:待定)
- 7.25 类型泛型数学 <tgmath.h> (页码:待定)
- F.10.1.7 tan 函数 (页码:待定)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.4.7 tan 函数 (页码:175)
- 7.25 类型泛型数学 <tgmath.h> (页码:272-273)
- F.10.1.7 tan 函数 (页码:378)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.4.7 tan 函数 (页码:240)
- 7.25 类型泛型数学 <tgmath.h> (页码:373-375)
- F.10.1.7 tan 函数 (页码:519)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.4.7 tan 函数 (页码:220)
- 7.22 类型泛型数学 <tgmath.h> (页码:335-337)
- F.9.1.7 tan 函数 (页码: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
|