- cmath[meta header]
- std[meta namespace]
- function[meta id-type]
- [mathjax enable]
namespace std {
float acos(float x);
double acos(double x);
long double acos(long double x);
double acos(Integral x); // C++11 から
float acosf(float x); // C++17 から
long double acosl(long double x); // C++17 から
}- Integral[italic]
算術型の逆余弦(アークコサイン、arc cosine)を求める。
acos()は、余弦を表すcos()の逆関数である。$\cos(\mathrm{Arccos}x) = x$、$\mathrm{Arccos}(\cos x) = x ~ (x \in [0, \pi])$である。
引数 x の逆余弦を主値 [0, π] の範囲で返す。(単位はラジアン)
x が [-1.0, 1.0] の範囲外だった場合は定義域エラーとなり、戻り値は処理系定義である。(備考参照)
- $$ f(x) = \mathrm{Arccos}~ x $$
- 定義域エラーが発生した場合の挙動については、
<cmath>を参照。 - C++11 以降では、処理系が IEC 60559 に準拠している場合(
std::numeric_limits<T>::is_iec559() != false)、以下の規定が追加される。x = 1の場合、戻り値は+0となる。x > |1|の場合、戻り値は quiet NaN となり、FE_INVALID(無効演算浮動小数点例外)が発生する。
#include <cmath>
#include <iostream>
int main() {
std::cout << std::fixed;
std::cout << "acos(0.0) = " << std::acos(0.0) << std::endl;
std::cout << "acos(0.5) = " << std::acos(0.5) << std::endl;
std::cout << "acos(1/√2) = " << std::acos(1.0 / std::sqrt(2.0)) << std::endl;
std::cout << "acos(√3/2) = " << std::acos(std::sqrt(3.0) / 2.0) << std::endl;
std::cout << "acos(1.0) = " << std::acos(1.0) << std::endl;
std::cout << "acos(-1.0) = " << std::acos(-1.0) << std::endl; // 円周率
}- std::acos[color ff0000]
- std::sqrt[link sqrt.md]
- std::fixed[link ../ios/fixed.md]
acos(0.0) = 1.570796
acos(0.5) = 1.047198
acos(1/√2) = 0.785398
acos(√3/2) = 0.523599
acos(1.0) = 0.000000
acos(-1.0) = 3.141593
- C++03
- C++11
- Clang: 1.9, 2.9, 3.1
- GCC: 3.4.6, 4.2.4, 4.3.5, 4.4.5, 4.5.1, 4.5.2, 4.6.1, 4.7.0
- GCC, C++11 mode: 4.3.4, 4.4.5, 4.5.2, 4.6.1, 4.7.0
- ICC: 10.1, 11.0, 11.1, 12.0
- Visual C++: 2003, 2005, 2008, 2010
特定の環境で constexpr 指定されている場合がある。(独自拡張)
- GCC 4.6.1 以上
以下のマクローリン級数を適当な次数で打ち切ることで近似的に求めることができる。
また、逆正接関数と逆余接関数の和は π / 2 なので asin から求めることができる。