std::acos, std::acosf, std::acosl
来自cppreference.com
在标头 <cmath> 定义 | ||
(1) | ||
float acos (float num ); double acos (double num ); | (C++23 前) | |
/* 浮点数类型 */ acos (/* 浮点数类型 */ num ); | (C++23 起) (C++26 起 constexpr) | |
float acosf(float num ); | (2) | (C++11 起) (C++26 起为 constexpr) |
longdouble acosl(longdouble num ); | (3) | (C++11 起) (C++26 起为 constexpr) |
SIMD 重载(C++26 起) | ||
在标头 <simd> 定义 | ||
template</*math-floating-point*/ V > constexpr/*deduced-simd-t*/<V> | (S) | (C++26 起) |
额外重载(C++11 起) | ||
在标头 <cmath> 定义 | ||
template<class Integer > double acos ( Integer num ); | (A) | (C++26 起为 constexpr) |
1-3) 计算 num 的弧(反)余弦主值。标准库提供所有以无 cv 限定的浮点数类型作为形参的类型的
std::acos
重载。(C++23 起)S) SIMD 重载对 v_num 实施逐元素 std::acos 。
| (C++26 起) |
A) 为所有整数类型提供额外重载,将它们当做 double。 | (C++11 起) |
目录 |
[编辑]参数
num | - | 浮点数或整数 |
[编辑]返回值
如果没有发生错误,那么返回 num 在范围 [0, π] 中的弧(反)余弦(arccos(num))。
如果发生定义域错误,那么返回值由实现定义(在受支持平台上是 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"<<'\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
[编辑]参阅
(C++11)(C++11) | 计算反正弦(arcsin(x)) (函数) |
(C++11)(C++11) | 计算反正切(arctan(x)) (函数) |
(C++11)(C++11) | 反正切,用符号确定象限 (函数) |
(C++11)(C++11) | 计算余弦(cos(x)) (函数) |
(C++11) | 计算复数的反余弦(arccos(z)) (函数模板) |
应用函数 std::acos 到 valarray 的每个元素 (函数模板) | |
acos 的 C 文档 |