C++ Valarray Library - acos Function

Complete Python Prime Pack for 2023

9 Courses     2 eBooks

Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

Java Prime Pack 2023

8 Courses     2 eBooks

Description

It returns a valarray containing the principal values of the arc cosine of all the elements of x, expressed in radians, in the same order.

Declaration

Following is the declaration for std::acos function.

```template<class T> valarray<T> acos (const valarray<T>& x);
```

C++11

```template<class T> valarray<T> acos (const valarray<T>& x);
```

Parameters

x − It is containing elements of a type for which the unary function abs is defined.

Return Value

It returns a valarray containing the principal values of the arc cosine of all the elements of x, expressed in radians, in the same order.

Exceptions

Basic guarantee − if any operation performed on the elements throws an exception.

Data races

All elements effectively copied are accessed.

Example

In below example explains about std::acos function.

```#include <iostream>
#include <cstddef>
#include <cmath>
#include <valarray>

int main () {
double val[] = {0.10, 0.25, 0.50, 0.75, 1.5};
std::valarray<double> foo (val,5);

std::valarray<double> bar = acos (foo);

std::cout << "foo:";
for (std::size_t i=0; i<foo.size(); ++i)
std::cout << ' ' << foo[i];
std::cout << '\n';

std::cout << "bar:";
for (std::size_t i=0; i<bar.size(); ++i)
std::cout << ' ' << bar[i];
std::cout << '\n';

return 0;
}
```

Let us compile and run the above program, this will produce the following result −

```foo: 0.1 0.25 0.5 0.75 1.5
bar: 1.47063 1.31812 1.0472 0.722734 nan
```
valarray.htm