sin, sinf, sinl

< c‎ | numeric‎ | math
在标头 <math.h> 定义
float       sinf( float arg );
(1) (C99 起)
double      sin( double arg );
(2)
long double sinl( long double arg );
(3) (C99 起)
_Decimal32  sind32( _Decimal32 arg );
(4) (C23 起)
_Decimal64  sind64( _Decimal64 arg );
(5) (C23 起)
_Decimal128 sind128( _Decimal128 arg );
(6) (C23 起)
在标头 <tgmath.h> 定义
#define sin( arg )
(7) (C99 起)
1-3) 计算 arg(以弧度度量)的正弦。
4) 泛型宏:若实参拥有 long double 类型,则调用 sinl。否则,若实参拥有整数类型或 double 类型,则调用 sin。否则调用 sinf 。若实参是复数,则该宏调用对应的复函数(csinfcsincsinl)。

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

(C23 起)

参数

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_MINFLT_MINLDBL_MIN 的值。

示例

#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("sin(pi/6) = %f\n", sin(pi / 6));
    printf("sin(pi/2) = %f\n", sin(pi / 2));
    printf("sin(-3*pi/4) = %f\n", sin(-3 * pi / 4));
 
    // 特殊值
    printf("sin(+0) = %f\n", sin(0.0));
    printf("sin(-0) = %f\n", sin(-0.0));
 
    // 错误处理
    feclearexcept(FE_ALL_EXCEPT);
    printf("sin(INFINITY) = %f\n", sin(INFINITY));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

可能的输出:

sin(pi/6) = 0.500000
sin(pi/2) = 1.000000
sin(-3*pi/4) = -0.707107
sin(+0) = 0.000000
sin(-0) = -0.000000
sin(INFINITY) = -nan
    FE_INVALID raised

引用

  • C23 标准(ISO/IEC 9899:2024):
  • 7.12.4.6 The sin functions (第 TBD 页)
  • 7.27 Type-generic math <tgmath.h> (第 TBD 页)
  • F.10.1.6 The sin functions (第 TBD 页)
  • C17 标准(ISO/IEC 9899:2018):
  • 7.12.4.6 The sin functions (第 175 页)
  • 7.25 Type-generic math <tgmath.h> (第 272-273 页)
  • F.10.1.6 The sin functions (第 378 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.12.4.6 The sin functions (第 239-240 页)
  • 7.25 Type-generic math <tgmath.h> (第 373-375 页)
  • F.10.1.6 The sin functions (第 519 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.12.4.6 The sin functions (第 220 页)
  • 7.22 Type-generic math <tgmath.h> (第 335-337 页)
  • F.9.1.6 The sin functions (第 456 页)
  • C89/C90 标准(ISO/IEC 9899:1990):
  • 4.5.2.6 The sin function

参阅

(C99)(C99)
计算余弦(cos(x)
(函数)
(C99)(C99)
计算正切(tan(x)
(函数)
(C99)(C99)
计算反正弦(arcsin(x)
(函数)
(C99)(C99)(C99)
计算复数正弦
(函数)