Namespaces
Variants

acos, acosf, acosl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float acosf ( float arg ) ;
(1) (C99 起)
double acos ( double arg ) ;
(2)
long double acosl ( long double arg ) ;
(3) (C99 起)
_Decimal32  acosd32 ( _Decimal32 arg ) ;
(4) (C23 起)
_Decimal64  acosd64 ( _Decimal64 arg ) ;
(5) (C23 起)
_Decimal128 acosd128 ( _Decimal128 arg ) ;
(6) (C23 起)
定义于头文件 <tgmath.h>
#define acos( arg )
(7) (C99 起)
1-6) 计算 arg 的反余弦主值。
7) 类型泛型宏:若参数类型为 long double ,则调用 (3) ( acosl );否则,若参数为整数类型或 double 类型,则调用 (2) ( acos );否则调用 (1) ( acosf )。若参数为复数,则宏调用对应的复数函数( cacosf cacos cacosl )。

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

(since C23)

目录

参数

arg - 浮点数值

返回值

如果未发生错误,则返回 arg 的反余弦值( arccos(arg) ),其值域为 [0 ; π]

如果发生定义域错误,将返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果由于下溢发生范围错误,将返回正确结果(舍入后)。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

arg 超出 [-1.0; 1.0] 范围时会发生定义域错误。

如果实现支持 IEEE 浮点算术 (IEC 60559):

  • 若参数为 +1 ,则返回数值 +0
  • |arg| > 1 ,则发生定义域错误并返回 NaN;
  • 若参数为 NaN,则返回 NaN。

示例

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
int main(void)
{
    printf("acos(-1) = %f\n", acos(-1));
    printf("acos(0.0) = %f 2*acos(0.0) = %f\n", acos(0), 2 * acos(0));
    printf("acos(0.5) = %f 3*acos(0.5) = %f\n", acos(0.5), 3 * acos(0.5));
    printf("acos(1) = %f\n", acos(1));
    // 错误处理
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("acos(1.1) = %f\n", acos(1.1));
    if (errno == EDOM)
        perror("    errno == EDOM");
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

可能的输出:

acos(-1) = 3.141593
acos(0.0) = 1.570796 2*acos(0.0) = 3.141593
acos(0.5) = 1.047198 3*acos(0.5) = 3.141593
acos(1) = 0.000000
acos(1.1) = nan
    errno == EDOM: Numerical argument out of domain
    FE_INVALID raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.4.1 acos 函数 (p: TBD)
  • 7.25 类型泛型数学 <tgmath.h> (p: TBD)
  • F.10.1.1 acos 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.4.1 acos 函数 (p: 173)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.1.1 acos 函数 (p: 378)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.4.1 acos 函数 (p: 238)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.1.1 acos 函数 (p: 518)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.4.1 acos函数(第218页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.1.1 acos函数(第455页)
  • C89/C90 标准 (ISO/IEC 9899:1990):
  • 4.5.2.1 acos 函数

参考

(C99) (C99)
计算反正弦( arcsin(x)
(函数)
(C99) (C99)
计算反正切( arctan(x)
(函数)
使用符号确定象限计算反正切
(函数)
(C99) (C99)
计算余弦( cos(x)
(函数)
(C99) (C99) (C99)
计算复数反余弦
(函数)
C++ 文档 关于 acos