std::tan, std::tanf, std::tanl

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       tan (float num );

double      tan (double num );

longdouble tan (longdouble num );
(C++23 前)
/* 浮点数类型 */
            tan (/* 浮点数类型 */ num );
(C++23 起)
(C++26 起 constexpr)
float       tanf(float num );
(2) (C++11 起)
(C++26 起为 constexpr)
longdouble tanl(longdouble num );
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载(C++26 起)
在标头 <simd> 定义
template</*math-floating-point*/ V >

constexpr/*deduced-simd-t*/<V>

            tan (const V& v_num );
(S) (C++26 起)
额外重载(C++11 起)
在标头 <cmath> 定义
template<class Integer >
double      tan ( Integer num );
(A) (C++26 起为 constexpr)
1-3) 计算 num(以弧度度量)的正切。标准库提供所有以无 cv 限定的浮点数类型作为形参的类型的 std::tan 重载。(C++23 起)
S) SIMD 重载对 v_num 实施逐元素 std::tan
(参见 math-floating-pointdeduced-simd-t 的定义。)
(C++26 起)
A) 为所有整数类型提供额外重载,将它们当做 double
(C++11 起)

目录

[编辑]参数

num - 以弧度表示角的浮点数或整数

[编辑]返回值

如果没有发生错误,那么返回 num 的正切(tan(num))。

如果 num 的绝对值很大,那么结果可能有较少或无有效数字。

(C++11 前)

如果发生定义域错误,那么返回值由实现定义(在受支持平台是 NaN)。

如果发生下溢导致的值域错误,那么返回(舍入后的)正确结果。

[编辑]错误处理

报告 math_errhandling 中指定的错误。

如果实现支持 IEEE 浮点数算术(IEC 60559),那么

  • 如果实参是 ±0,那么返回不修改的该值
  • 如果实参是 ±∞,那么返回 NaN 并引发 FE_INVALID
  • 如果实参是 NaN,那么返回 NaN

[编辑]注解

(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 ONconstdouble pi =std::acos(-1);// 或用 C++20 的 std::numbers::pi   int main(){// 典型用法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°   // 特殊值std::cout<<"tan(+0) = "<< std::tan(0.0)<<'\n'<<"tan(-0) = "<< std::tan(-0.0)<<'\n';   // 错误处理std::feclearexcept(FE_ALL_EXCEPT);   std::cout<<"tan(INFINITY) = "<< std::tan(INFINITY)<<'\n';if(std::fetestexcept(FE_INVALID))std::cout<<" 发生 FE_INVALID\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

[编辑]参阅

(C++11)(C++11)
计算正弦(sin(x)
(函数)[编辑]
(C++11)(C++11)
计算余弦(cos(x)
(函数)[编辑]
(C++11)(C++11)
计算反正切(arctan(x)
(函数)[编辑]
计算复数的正切(tan(z)
(函数模板)[编辑]
应用函数 std::tan 到 valarray 的每个元素
(函数模板)[编辑]
tan 的 C 文档
close