C++ Valarray::acos Function
The C++ Valarray::acos() function calculates the arc cosine of each element's value in a valarray and produces a valarray that contains the results.
The C++ acos() function returns the inverse cosine of an integer in radians. For each element, it makes a single call to the cmath's acos() method, which it overloads.
Syntax
Following is the syntax for C++ Valarray::acos Function −
acos (const valarray<T>& x);
Parameters
x − It is containing elements of a type for which the unary function acos is defined.
Examples
Example 1
Let's look into the following example, where we are going to use the acos() function and retrieving the output.
#include <iostream>
#include <valarray>
using namespace std;
int main() {
valarray<double>
varr = { 1, 0.25, 0.75, 0.5, 0 };
valarray<double> valarraay1;
valarraay1 = acos(varr);
cout << "The acos Valarray"
<< " Value : "
<< endl;
for (double& x : valarraay1) {
cout << x << " ";
}
cout << endl;
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
The acos Valarray Value : 0 1.31812 0.722734 1.0472 1.5708
Example 2
In the following example,we are going to use the acos() function and retrieving the output with comparison of original and acos Valarray.
#include <iostream>
#include <valarray>
using namespace std;
int main() {
valarray<int> myvalarr = { 12, 143, 3, 7, 9 };
cout << "The Orignal Valarray : ";
for (int& ele : myvalarr)
cout << ele << " ";
valarray<int> acosvalarray = acos(myvalarr);
cout << "\nThe acos Valarray : ";
for (int& ele : acosvalarray)
cout << ele << " ";
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
The Orignal Valarray : 12 143 3 7 9 The acos Valarray : -2147483648 -2147483648 -2147483648 -2147483648 -2147483648
Example 3
Following is the another scenario, where we are going to use the acos() function with integral type and retrieving the output in radians, degrees.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x = -0.75;
double result;
result = acos(x);
cout << "acos(x) = " << result << " radians" << endl;
cout << "acos(x1) = " << result*180/3.1415 << " degrees";
return 0;
}
Output
Let us compile and run the above program, this will produce the following result −
acos(x) = 1.5708 radians acos(x1) = 90.0027 degrees