C++11标准模板(STL)- 常用数学函数 - 计算正切 (tan(x))(std::tan, std::tanf, std::tanl)

常用数学函数

计算正切 (tan(x))

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

定义于头文件 <math.h>

float       tanf( float arg );

(1)(C99 起)

double      tan( double arg );

(2)

long double tanl( long double arg );

(3)(C99 起)

定义于头文件 <tgmath.h>

#define tan( arg )

(4)(C99 起)

1-3) 计算 arg (以弧度度量)的正切。

4) 泛型宏:若参数拥有 long double 类型,则调用 tanl 。否则,若参数拥有整数类型或 double 类型,则调用 tan 。否则调用 tanf 。若参数为复数,则宏调用对应的复数函数( ctanf 、 ctan 、 ctanl )。

参数

arg-以弧度表示角的浮点值

返回值

若不出现错误,则返回 arg 的正切( tan(arg) )。

arg 的绝对值很大,则结果可能有较少或无有效数字。

(C99 前)

若出现定义域错误,则返回实现定义值(受支持平台为 NaN )。

若发生下溢所致的值域错误,则返回(舍入后的)正确结果。

错误处理

报告 math_errhandling 中指定的错误。

若实现支持 IEEE 浮点算数( IEC 60559 ),则

  • 若参数为 ±0 ,则返回不修改的参数
  • 若参数为 ±∞ ,则返回 NaN 并引发 FE_INVALID
  • 若参数为 NaN ,则返回 NaN

注意

C 中参数为无限大的情况未被指定为定义域错误,但它被定义为 POSIX 中的定义域错误

函数在 π(1/2 + n) 有数学上的极点;然而无常用浮点表示能准确表示 π/2 ,故而没有值使得极点错误出现。

调用示例

#include <iostream>
#include <cstdlib>
#include <typeinfo>
#include <cinttypes>
#include <cmath>
#include <math.h>
#include <tgmath.h>

int main()
{
    //计算正切 (tan(x))。
    const float fNumber = std::acos(-1);
    std::cout << "typeid(float).name():   " << typeid(float).name() << std::endl;
    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << fNumber / i << "):   "
                  << std::tan(fNumber / i) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << - fNumber / i << "):   "
                  << std::tan(- fNumber / i) << std::endl;
    }
    std::cout << std::endl;

    const double dNumber = std::acos(-1);
    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << dNumber / i << "):   "
                  << std::tan(dNumber / i) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << - dNumber / i << "):   "
                  << std::tan(- dNumber / i) << std::endl;
    }
    std::cout << std::endl;

    const long double ldNumber = std::acos(-1);
    std::cout << "typeid(long double).name():   " << typeid(long double).name() << std::endl;
    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << ldNumber / i << "):   "
                  << std::tan(ldNumber / i) << std::endl;
    }
    std::cout << std::endl;

    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::tan(" << - ldNumber / i << "):   "
                  << std::tan(- ldNumber / i) << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

输出

typeid(float).name():   f
std::tan(3.14159):   8.74228e-008
std::tan(1.5708):   -2.28773e+007
std::tan(1.0472):   1.73205
std::tan(0.785398):   1
std::tan(0.628319):   0.726543
std::tan(0.523599):   0.57735
std::tan(0.448799):   0.481575
std::tan(0.392699):   0.414214
std::tan(0.349066):   0.36397
std::tan(0.314159):   0.32492

std::tan(-3.14159):   -8.74228e-008
std::tan(-1.5708):   2.28773e+007
std::tan(-1.0472):   -1.73205
std::tan(-0.785398):   -1
std::tan(-0.628319):   -0.726543
std::tan(-0.523599):   -0.57735
std::tan(-0.448799):   -0.481575
std::tan(-0.392699):   -0.414214
std::tan(-0.349066):   -0.36397
std::tan(-0.314159):   -0.32492

std::tan(3.14159):   -1.22461e-016
std::tan(1.5708):   1.63318e+016
std::tan(1.0472):   1.73205
std::tan(0.785398):   1
std::tan(0.628319):   0.726543
std::tan(0.523599):   0.57735
std::tan(0.448799):   0.481575
std::tan(0.392699):   0.414214
std::tan(0.349066):   0.36397
std::tan(0.314159):   0.32492

std::tan(-3.14159):   1.22461e-016
std::tan(-1.5708):   -1.63318e+016
std::tan(-1.0472):   -1.73205
std::tan(-0.785398):   -1
std::tan(-0.628319):   -0.726543
std::tan(-0.523599):   -0.57735
std::tan(-0.448799):   -0.481575
std::tan(-0.392699):   -0.414214
std::tan(-0.349066):   -0.36397
std::tan(-0.314159):   -0.32492

typeid(long double).name():   e
std::tan(3.14159):   -1.22461e-016
std::tan(1.5708):   1.63318e+016
std::tan(1.0472):   1.73205
std::tan(0.785398):   1
std::tan(0.628319):   0.726543
std::tan(0.523599):   0.57735
std::tan(0.448799):   0.481575
std::tan(0.392699):   0.414214
std::tan(0.349066):   0.36397
std::tan(0.314159):   0.32492

std::tan(-3.14159):   1.22461e-016
std::tan(-1.5708):   -1.63318e+016
std::tan(-1.0472):   -1.73205
std::tan(-0.785398):   -1
std::tan(-0.628319):   -0.726543
std::tan(-0.523599):   -0.57735
std::tan(-0.448799):   -0.481575
std::tan(-0.392699):   -0.414214
std::tan(-0.349066):   -0.36397
std::tan(-0.314159):   -0.32492

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值