Tan
定义于头文件 <cmath>
中。
描述
计算num
(以弧度为单位)的正切。该库为所有 cv-unqualified 浮点类型提供了 std::tan
的重载,作为参数 num
的类型。 (自 C++23 起)
附加重载 为所有整数类型提供,它们被视为 double。 (自 C++11 起)
声明
- C++23
- C++11
// 1)
/* floating-point-type */ tan( /* floating-point-type */ num );
// 2)
float tanf( float num );
// 3)
long double tanl( long double num );
// 4)
template< class Integer >
double tan ( Integer num );
// 1)
float tan ( float num );
// 2)
double tan ( double num );
// 3)
long double tan ( long double num );
// 4)
float tanf( float num );
// 5)
long double tanl( long double num );
// 6)
template< class Integer >
double tan ( Integer num );
参数
num
- 表示弧度角值的浮点或整数值
返回值
如果未发生错误,则返回 num
的正切 (tan(num))。
如果 num
的幅度很大,结果可能意义不大或没有意义。(直到 C++11)
如果发生域错误,则返回实现定义的值(如果支持,返回 NaN)。
如果因下溢导致范围错误,则返回正确结果(舍入后)。
错误处理
错误按 math_errhandling 中指定的方式报告。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果参数是 ±0
,则原样返回
如果参数是 ±∞
,则返回 NaN 并引发 FE_INVALID
如果参数是 NaN,则返回 NaN
备注
参数为无穷大的情况在 C 语言中(C++ 遵循 C 语言)未指定为域错误,但在 POSIX 中将其定义为域错误。
函数在 π(1/2 + n)
处有数学极点;但是,没有常见的浮点表示能够精确表示 π/2
,因此没有参数值会导致极点错误。
额外的重载不需要完全按照额外重载提供。它们只需要足以确保对于其整数类型的参数 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