##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::acos

From cppreference.com
< cpp‎ | numeric‎ | math

Defined in header `<cmath>` |  |
float acos( float arg ); | (1) |
double acos( double arg ); | (2) |
long double acos( long double arg ); | (3) |
double acos( Integral arg ); | (4) | (since C++11)

Computes the principal value of the arc cosine of `arg`.

4) A set of overloads or a function template accepting an argument of any integral type. Equivalent to 2) (the argument is cast to double).

### [edit] Parameters

arg | - | value of a floating-point or Integral type

### [edit] Return value

If no errors occur, the arc cosine of `arg` (arccos(arg)) in the range [0 ; π], is returned.

If a domain error occurs, an implementation-defined value is returned (NaN where supported)

If a range error occurs due to underflow, the correct result (after rounding) is returned.

### [edit] Error handling

Errors are reported as specified in math_errhandling

Domain error occurs if `arg` is outside the range `[-1.0; 1.0]`

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

- If the argument is +1, the value `+0` is returned.
- If |arg| > 1, a domain error occurs and NaN is returned.
- if the argument is NaN, NaN is returned

### [edit] Example

Run this code

    #include <cmath>
    #include <iostream>
    #include <cerrno>
    #include <cfenv>
    #include <cstring>

    #pragma STDC FENV_ACCESS ON
    int main()
    {
        std::cout << "acos(-1) = " << acos(-1) << '\n'
                  << "acos(0.0) = " << acos(0.0) << " 2*acos(0.0) = " << 2*acos(0) << '\n'
                  << "acos(0.5) = " << acos(0.5) << " 3*acos(0.5) = " << 3*acos(0.5) << '\n'
                  << "acos(1) = " << acos(1) << '\n';
        // error handling
        errno = 0; std::feclearexcept(FE_ALL_EXCEPT);
        std::cout << "acos(1.1) = " << 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';
    }

Output:

    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

### [edit] See also

asin | computes arc sine (arcsin(x)) (function) [edit]
atan | computes arc tangent (arctan(x)) (function) [edit]
atan2 | arc tangent, using signs to determine quadrants (function) [edit]
cos | computes cosine (cos(x)) (function) [edit]
acos(std::complex) (C++11) | computes arc cosine of a complex number (arccos(z)) (function template) [edit]
acos(std::valarray) | applies the function std::acos to each element of valarray (function template) [edit]
C documentation for acos

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/acos&oldid=70519"