acos() function in C++ STL

acos() is an inbuilt function in C++ STL and it’s the same as the inverse of cosine in maths. The acos() function returns the values in the range of [0, ?] that is an angle in radian.

Syntax :

acos(data_type x)

Parameters : This function accepts one mandatory parameter x which specifies the value whose inverse of cosine should be computed. x must be in the range of [-1, 1] to find valid output as [0, ?], else acos(x) function returns NaN(Not a Number) . The parameter x can be of double, float or long double datatype.

Return : The function returns angles in radians in the range of  [0,?]. It is the counterclockwise angle which is measured in radian.

Program 1:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// the acos() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    double x = 1.0;
 
    // Function call to calculate acos(x) value
    double result = acos(x);
 
    cout << "acos(1.0) = " << result <<
                               " radians" << endl;
    cout << "acos(1.0) = " << result * 180 / 3.141592
         << " degrees" << endl;
 
    return 0;
}

chevron_right


Output

acos(1.0) = 0 radians
acos(1.0) = 0 degrees

Program 2:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate
// the acos() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    double result;
    int x = -1;
 
    // Function call to calculate acos(x) value
    result = acos(x);
 
    cout << "acos(-1) = " << result
        << " radians" << endl;
    cout << "acos(-1) = " << result * 180 / 3.141592
        << " degrees" << endl;
 
    return 0;
}

chevron_right


Output

acos(-1) = 3.14159 radians
acos(-1) = 180 degrees

Errors and Exceptions:

  • The function returns no matching function for call to error when a string or character is passed as an argument.
  • The function returns nan when a out of domain(domain [-1,1]) number is passed as an argument.

Below programs illustrate the errors and exceptions of the above method:

Program 3:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the acos()
// function errors and exceptions
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    double result;
    string x = "gfg";
    result = acos(x);
 
    cout << "acos(x) = " << result
        << " radians" << endl;
    cout << "acos(x) = " << result * 180 / 3.141592
        << " degrees" << endl;
 
    return 0;
}

chevron_right


Ouput:

prog.cpp:10:17: error: no matching function for call to 'acos(std::__cxx11::string&)'
  result = acos(x);

When argument x >1 or x<-1 it will give nan(not a number).

Program 4:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate the
// acos() function errors and exceptions
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    double x = 3.7, result;
 
    // Function call to calculate acos(x) value
    result = acos(x);
 
    cout << "acos(3.7) = " << result
        << " radians" << endl;
    cout << "acos(3.7) = " << result * 180 / 3.141592
        << " degrees" << endl;
 
    return 0;
}

chevron_right


Output:

acos(3.7) = nan radians
acos(3.7) = nan degrees

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : badger_666

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.