C++11标准模板(STL)- 常用数学函数 - 计算正弦 (sin(x))(std::sin, std::sinf, std::sinl)

常用数学函数

计算正弦 (sin(x))

std::sin, 
std::sinf, 
std::sinl

定义于头文件 <math.h>

float       sinf( float arg );

(1)(C99 起)

double      sin( double arg );

(2)

long double sinl( long double arg );

(3)(C99 起)

定义于头文件 <tgmath.h>

#define sin( arg )

(4)(C99 起)

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

4) 泛型宏:若参数拥有 long double 类型,则调用 sinl 。否则,若参数拥有整数类型或 double 类型,则调用 sin 。否则调用 sinf 。若参数是复数,则该宏调用对应的复函数( csinf 、 csin 、 csinl )。

参数

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

返回值

若不发生错误,则返回 arg 的正弦( sin(arg) ),于范围 [-1 ; +1] 中。

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

(C99 前)

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

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

错误处理

报告 math_errhandling 中指定的错误。

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

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

注意

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

POSIX 亦指定在溢出的情况下,返回不修改的 arg ,而且若不支持如此,则返回实现定义的不大于 DBL_MIN 、 FLT_MIN 及 LDBL_MIN 的值。

调用示例

#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、付费专栏及课程。

余额充值