Namespaces
Variants

std:: acos, std:: acosf, std:: acosl

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float acos ( float num ) ;

double acos ( double num ) ;

long double acos ( long double num ) ;
(C++23 前)
/*floating-point-type*/
acos ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float acosf ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double acosl ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

acos ( const V & v_num ) ;
(S) (C++26 起)
附加重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double acos ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 num 的反余弦主值。 库为所有 cv 未限定浮点类型提供了 std::acos 的重载,作为参数类型。 (since C++23)
S) SIMD 重载对 v_num 执行逐元素的 std::acos 运算。
(定义参见 math-floating-point deduced-simd-t
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型将被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

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

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

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

错误处理

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

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

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

  • 如果参数为 +1 ,则返回值 +0
  • 如果 |num| > 1 ,将发生定义域错误并返回 NaN。
  • 如果参数为 NaN,则返回 NaN。

注释

额外的重载并不要求完全按照 (A) 的形式提供。只需确保对于整数类型的实参 num std :: acos ( num ) 能够产生与 std :: acos ( static_cast < double > ( num ) ) 相同的效果即可。

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
    std::cout << "acos(-1) = " << std::acos(-1) << '\n'
              << "acos(0.0) = " << std::acos(0.0) << '\n'
              << "2*acos(0.0) = " << 2 * std::acos(0) << '\n'
              << "acos(0.5) = " << std::acos(0.5) << '\n'
              << "3*acos(0.5) = " << 3 * std::acos(0.5) << '\n'
              << "acos(1) = " << std::acos(1) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "acos(1.1) = " << std::acos(1.1) << '\n';
    if (errno == EDOM)
        std::cout << "    errno == EDOM: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised" << '\n';
}

输出:

acos(-1) = 3.14159
acos(0.0) = 1.5708
2*acos(0.0) = 3.14159
acos(0.5) = 1.0472
3*acos(0.5) = 3.14159
acos(1) = 0
acos(1.1) = nan
    errno == EDOM: Numerical argument out of domain
    FE_INVALID raised

参见

(C++11) (C++11)
计算反正弦( arcsin(x)
(函数)
(C++11) (C++11)
计算反正切( arctan(x)
(函数)
(C++11) (C++11)
使用符号确定象限的反正切
(函数)
(C++11) (C++11)
计算余弦( cos(x)
(函数)
计算复数的反余弦( arccos(z)
(函数模板)
对 valarray 的每个元素应用函数 std::acos
(函数模板)
C 文档 关于 acos