C++11标准模板(STL)- 常用数学函数 - 计算双曲余弦 (ch(x))(std::cosh, std::coshf, std::coshl)

常用数学函数

计算双曲余弦 (ch(x))

std::cosh, 
std::coshf, 
std::coshl

定义于头文件 <math.h>

float       coshf( float arg );

(1)(C99 起)

double      cosh( double arg );

(2)

long double coshl( long double arg );

(3)(C99 起)

定义于头文件 <tgmath.h>

#define cosh( arg )

(4)(C99 起)

1-3) 计算 arg 的双曲余弦。

4) 泛型宏:若参数拥有 long double 类型,则调用 coshl 。否则,若参数拥有整数类型或 double 类型,则调用 cosh 。否则调用 coshf 。若参数为复数,则宏调用对应的复数函数( ccoshf 、 ccosh 、 ccoshl )。

参数

arg-表示双曲角的浮点值

返回值

若不出现错误,则返回 arg 的双曲余弦( cosh(arg) 或

earg
+e-arg
2

)。

若出现上溢所致的值域错误,则返回 +HUGE_VAL+HUGE_VALF+HUGE_VALL

错误处理

报告 math_errhandling 中指定的错误。

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

  • 若参数为 ±0 ,则返回 1
  • 若参数为 ±∞ ,则返回 +∞
  • 若参数为 NaN ,则返回 NaN

注意

对于 IEEE 兼容的 double 类型,若 |arg| > 710.5 ,则 cosh(arg) 上溢。

调用示例

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

int main()
{
    //1-3) 计算 arg 的双曲余弦。
    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::cosh(" << fNumber / i << "):   "
                  << std::cosh(fNumber / i) << std::endl;
    }
    std::cout << std::endl;

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

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

    for (int i = 1; i <= 10; i += 1)
    {
        std::cout << "std::cosh(" << - dNumber / i << "):   "
                  << std::cosh(- 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::cosh(" << ldNumber / i << "):   "
                  << std::cosh(ldNumber / i) << std::endl;
    }
    std::cout << std::endl;

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

    return 0;
}

输出

typeid(float).name():   f
std::cosh(3.14159):   11.592
std::cosh(1.5708):   2.50918
std::cosh(1.0472):   1.60029
std::cosh(0.785398):   1.32461
std::cosh(0.628319):   1.20397
std::cosh(0.523599):   1.14024
std::cosh(0.448799):   1.10241
std::cosh(0.392699):   1.0781
std::cosh(0.349066):   1.06154
std::cosh(0.314159):   1.04976

std::cosh(-3.14159):   11.592
std::cosh(-1.5708):   2.50918
std::cosh(-1.0472):   1.60029
std::cosh(-0.785398):   1.32461
std::cosh(-0.628319):   1.20397
std::cosh(-0.523599):   1.14024
std::cosh(-0.448799):   1.10241
std::cosh(-0.392699):   1.0781
std::cosh(-0.349066):   1.06154
std::cosh(-0.314159):   1.04976

typeid(double).name():   d
std::cosh(3.14159):   11.592
std::cosh(1.5708):   2.50918
std::cosh(1.0472):   1.60029
std::cosh(0.785398):   1.32461
std::cosh(0.628319):   1.20397
std::cosh(0.523599):   1.14024
std::cosh(0.448799):   1.10241
std::cosh(0.392699):   1.0781
std::cosh(0.349066):   1.06154
std::cosh(0.314159):   1.04976

std::cosh(-3.14159):   11.592
std::cosh(-1.5708):   2.50918
std::cosh(-1.0472):   1.60029
std::cosh(-0.785398):   1.32461
std::cosh(-0.628319):   1.20397
std::cosh(-0.523599):   1.14024
std::cosh(-0.448799):   1.10241
std::cosh(-0.392699):   1.0781
std::cosh(-0.349066):   1.06154
std::cosh(-0.314159):   1.04976

typeid(long double).name():   e
std::cosh(3.14159):   11.592
std::cosh(1.5708):   2.50918
std::cosh(1.0472):   1.60029
std::cosh(0.785398):   1.32461
std::cosh(0.628319):   1.20397
std::cosh(0.523599):   1.14024
std::cosh(0.448799):   1.10241
std::cosh(0.392699):   1.0781
std::cosh(0.349066):   1.06154
std::cosh(0.314159):   1.04976

std::cosh(-3.14159):   11.592
std::cosh(-1.5708):   2.50918
std::cosh(-1.0472):   1.60029
std::cosh(-0.785398):   1.32461
std::cosh(-0.628319):   1.20397
std::cosh(-0.523599):   1.14024
std::cosh(-0.448799):   1.10241
std::cosh(-0.392699):   1.0781
std::cosh(-0.349066):   1.06154
std::cosh(-0.314159):   1.04976

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值