Namespaces
Variants

tan, tanf, tanl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 )。

当且仅当实现预定义了 __STDC_IEC_60559_DFP__ (即实现支持十进制浮点数)时,函数 (4-6) 才会被声明。

(since C23)

目录

参数

arg - 表示以弧度为单位的角度值的浮点数值

返回值

如果未发生错误,则返回 arg 的正切值 ( tan(arg) )。

如果 arg 的幅值很大,则结果可能几乎没有意义或毫无意义。

(C99前)

如果发生定义域错误,将返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果实现支持 IEEE 浮点算术 (IEC 60559):

  • 若参数为 ±0,则不作修改直接返回;
  • 若参数为 ±∞,则返回 NaN 并引发 FE_INVALID
  • 若参数为 NaN,则返回 NaN。

注释

当参数为无穷大的情况在C语言中未被规定为定义域错误,但被定义为 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);
    // 典型用法
    printf("tan(pi*1/4) = %+f\n", tan(pi * 1 / 4)); //   45 度
    printf("tan(pi*3/4) = %+f\n", tan(pi * 3 / 4)); //  135 度
    printf("tan(pi*5/4) = %+f\n", tan(pi * 5 / 4)); // -135 度
    printf("tan(pi*7/4) = %+f\n", tan(pi * 7 / 4)); //  -45 度
    // 特殊值
    printf("tan(+0) = %f\n", tan(0.0));
    printf("tan(-0) = %f\n", tan(-0.0));
    // 错误处理
    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 函数集 (p: TBD)
  • 7.25 泛型数学 <tgmath.h> (p: TBD)
  • F.10.1.7 tan 函数集 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.4.7 tan 函数 (p: 175)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.1.7 tan 函数 (p: 378)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.4.7 tan 函数 (p: 240)
  • 7.25 类型泛型数学 <tgmath.h> (p: 373-375)
  • F.10.1.7 tan 函数 (p: 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++ 文档 for tan