C++11标准模板(STL)- 常用数学函数 - 计算余弦 (cos(x))(std::cos, std::cosf, std::cosl)

常用数学函数

计算余弦 (cos(x))

std::cos, 
std::cosf, 
std::cosl

定义于头文件 <math.h>

float       cosf( float arg );

(1)(C99 起)

double      cos( double arg );

(2)

long double cosl( long double arg );

(3)(C99 起)

定义于头文件 <tgmath.h>

#define cos( arg )

(4)(C99 起)

1-3) 计算 arg (以弧度计量)的余弦。

4) 泛型宏:若参数拥有 long double 类型,则调用 cosl 。否则,若参数拥有整数类型或 double 类型,则调用 cos 。否则,调用 cosf 、若参数为复数,则该宏调用对应的复函数( ccosf 、 ccos 、 ccosl )。

参数

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

返回值

若不出现错误,则返回 arg 的余弦( cos(arg) ),在范围 [-1 ; +1] 中。

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

(C99 前)

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

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

错误处理

报告 math_errhandling 中指定的错误。

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

  • 则若参数为 ±0 ,则结果为 1.0
  • 若参数为 ±∞ ,则返回 NaN 并引发 FE_INVALID
  • 若参数为 NaN ,则返回 NaN

注意

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

调用示例

#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::sin(" << fNumber / i << "):   "
                  << std::sin(fNumber / i) << std::endl;
    }
    std::cout << std::endl;

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

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

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

    return 0;
}

输出

typeid(float).name():   f
std::sin(3.14159):   -8.74228e-008
std::sin(1.5708):   1
std::sin(1.0472):   0.866025
std::sin(0.785398):   0.707107
std::sin(0.628319):   0.587785
std::sin(0.523599):   0.5
std::sin(0.448799):   0.433884
std::sin(0.392699):   0.382683
std::sin(0.349066):   0.34202
std::sin(0.314159):   0.309017

std::sin(-3.14159):   8.74228e-008
std::sin(-1.5708):   -1
std::sin(-1.0472):   -0.866025
std::sin(-0.785398):   -0.707107
std::sin(-0.628319):   -0.587785
std::sin(-0.523599):   -0.5
std::sin(-0.448799):   -0.433884
std::sin(-0.392699):   -0.382683
std::sin(-0.349066):   -0.34202
std::sin(-0.314159):   -0.309017

std::sin(3.14159):   1.22461e-016
std::sin(1.5708):   1
std::sin(1.0472):   0.866025
std::sin(0.785398):   0.707107
std::sin(0.628319):   0.587785
std::sin(0.523599):   0.5
std::sin(0.448799):   0.433884
std::sin(0.392699):   0.382683
std::sin(0.349066):   0.34202
std::sin(0.314159):   0.309017

std::sin(-3.14159):   -1.22461e-016
std::sin(-1.5708):   -1
std::sin(-1.0472):   -0.866025
std::sin(-0.785398):   -0.707107
std::sin(-0.628319):   -0.587785
std::sin(-0.523599):   -0.5
std::sin(-0.448799):   -0.433884
std::sin(-0.392699):   -0.382683
std::sin(-0.349066):   -0.34202
std::sin(-0.314159):   -0.309017

typeid(long double).name():   e
std::sin(3.14159):   1.22461e-016
std::sin(1.5708):   1
std::sin(1.0472):   0.866025
std::sin(0.785398):   0.707107
std::sin(0.628319):   0.587785
std::sin(0.523599):   0.5
std::sin(0.448799):   0.433884
std::sin(0.392699):   0.382683
std::sin(0.349066):   0.34202
std::sin(0.314159):   0.309017

std::sin(-3.14159):   -1.22461e-016
std::sin(-1.5708):   -1
std::sin(-1.0472):   -0.866025
std::sin(-0.785398):   -0.707107
std::sin(-0.628319):   -0.587785
std::sin(-0.523599):   -0.5
std::sin(-0.448799):   -0.433884
std::sin(-0.392699):   -0.382683
std::sin(-0.349066):   -0.34202
std::sin(-0.314159):   -0.309017

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值