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 起) (C++26 起为 constexpr) |
|
float tanf( float num ); |
(2) | (C++11 起) (C++26 起为 constexpr) |
long double tanl( long double num ); |
(3) | (C++11 起) (C++26 起为 constexpr) |
SIMD 重载 (C++26 起) |
||
定义于头文件 <simd> |
||
template< /*数学浮点类型*/ V > constexpr /*推导 SIMD 类型*/<V> |
(S) | (C++26 起) |
额外重载 (自 C++11 起) |
||
定义于头文件 <cmath> |
||
template< class Integer > double tan ( Integer num ); |
(A) | (C++26 起为 constexpr) |
1-3) 计算 num 的正切值(以弧度为单位)。 库为所有 cv-unqualified 浮点类型的参数提供
std::tan
的重载。(C++23 起)
S) SIMD 重载对 v_num 的每个元素执行
std::tan 。
|
(C++26 起) |
A) 为所有整数类型提供了额外的重载,它们被视为 double。
|
(C++11 起) |
目录 |
[编辑] 参数
num | - | 表示弧度角的浮点数或整数值 |
[编辑] 返回值
如果没有发生错误,返回 num 的正切值(tan(num))。
如果 num 的幅度很大,结果可能没有或几乎没有意义。 |
(C++11 前) |
如果发生域错误,则返回实现定义的值 (支持 NaN 时返回 NaN)。
如果因下溢发生范围错误,则返回正确结果(舍入后)。
[编辑] 错误处理
错误按 math_errhandling 指定的方式报告。
如果实现支持 IEEE 浮点运算 (IEC 60559),
- 如果参数为 ±0,则原样返回。
- 如果参数为 ±∞,则返回 NaN 并引发 FE_INVALID。
- 如果参数为 NaN,则返回 NaN。
[编辑] 注意
参数为无穷大的情况在 C 语言(C++ 遵循 C 语言)中未指定为域错误,但在 POSIX 中定义为域错误。
此函数在 π(1/2 + n) 处有数学上的极点;然而,没有常见的浮点表示能够精确表示 π/2,因此不存在导致极点错误的参数值。
不需要完全按照 (A) 提供额外的重载。它们只需足以确保对于其整数类型参数 num,std::tan(num) 具有与 std::tan(static_cast<double>(num)) 相同的效果。
[编辑] 示例
运行此代码
#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
[编辑] 参阅
(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
|