std::tan, std::tanf, std::tanl
来自 cppreference.cn
定义于头文件 <cmath> |
||
(1) | ||
float tan ( float num ); double tan ( double num ); |
(直到 C++23) | |
/*floating-point-type*/ tan ( /*floating-point-type*/ num ); |
(自 C++23 起) (constexpr 自 C++26 起) |
|
float tanf( float num ); |
(2) | (自 C++11 起) (constexpr 自 C++26 起) |
long double tanl( long double num ); |
(3) | (自 C++11 起) (constexpr 自 C++26 起) |
SIMD 重载 (自 C++26 起) |
||
定义于头文件 <simd> |
||
template< /*math-floating-point*/ V > constexpr /*deduced-simd-t*/<V> |
(S) | (自 C++26 起) |
附加重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double tan ( Integer num ); |
(A) | (constexpr 自 C++26 起) |
1-3) 计算 num 的正切值(以弧度为单位)。 库为所有 cv 限定符消除的浮点类型提供
std::tan
的重载,作为形参的类型。(自 C++23 起)
S) SIMD 重载对 v_num 执行逐元素的
std::tan 。
|
(自 C++26 起) |
A) 为所有整数类型提供额外的重载,它们被视为 double。
|
(自 C++11 起) |
目录 |
[edit] 参数
num | - | 浮点或整数值,表示以弧度为单位的角度 |
[edit] 返回值
如果没有错误发生,则返回 num 的正切值 (tan(num))。
如果 num 的量级很大,则结果可能几乎没有或没有意义。 |
(直到 C++11) |
如果发生域错误,则返回实现定义的数值(如果支持,则为 NaN)。
如果由于下溢而发生范围错误,则返回正确的结果(舍入后)。
[edit] 错误处理
错误按照 math_errhandling 中的规定报告。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 如果参数为 ±0,则原样返回。
- 如果参数为 ±∞,则返回 NaN 并引发 FE_INVALID。
- 如果参数为 NaN,则返回 NaN。
[edit] 注解
参数为无穷大的情况在 C (C++ 遵循 C 标准)中未指定为域错误,但在 POSIX 中定义为域错误。
该函数在 π(1/2 + n) 处有数学极点;但是,没有常见的浮点表示能够精确地表示 π/2,因此对于参数的任何值都不会发生极点错误。
不需要完全按照 (A) 的形式提供额外的重载。 它们只需要足以确保对于整数类型的参数 num,std::tan(num) 具有与 std::tan(static_cast<double>(num)) 相同的效果。
[edit] 示例
运行此代码
#include <cerrno> #include <cfenv> #include <cmath> #include <iostream> // #pragma STDC FENV_ACCESS ON const double pi = std::acos(-1); // or C++20's std::numbers::pi int main() { // typical usage std::cout << "tan(1*pi/4) = " << std::tan(1*pi/4) << '\n' // 45° << "tan(3*pi/4) = " << std::tan(3*pi/4) << '\n' // 135° << "tan(5*pi/4) = " << std::tan(5*pi/4) << '\n' // -135° << "tan(7*pi/4) = " << std::tan(7*pi/4) << '\n'; // -45° // special values std::cout << "tan(+0) = " << std::tan(0.0) << '\n' << "tan(-0) = " << std::tan(-0.0) << '\n'; // error handling std::feclearexcept(FE_ALL_EXCEPT); std::cout << "tan(INFINITY) = " << std::tan(INFINITY) << '\n'; if (std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
可能的输出
tan(1*pi/4) = 1 tan(3*pi/4) = -1 tan(5*pi/4) = 1 tan(7*pi/4) = -1 tan(+0) = 0 tan(-0) = -0 tan(INFINITY) = -nan FE_INVALID raised
[edit] 参见
(C++11)(C++11) |
计算正弦值 (sin(x)) (函数) |
(C++11)(C++11) |
计算余弦值 (cos(x)) (函数) |
(C++11)(C++11) |
计算反正切值 (arctan(x)) (函数) |
计算复数的正切值 (tan(z)) (函数模板) | |
将函数 std::tan 应用于 valarray 的每个元素 (函数模板) | |
C 文档 关于 tan
|