How to use asin() in C++

The built-in function asin() can be accessed using the cmath header file. It is used to calculate the inverse sine value of the parameter provided in radians. This is depicted in the diagram below:

C++ representation of the mathematical expression

Domain and range

The permitted values of the parameter (x) are from -1 to 1 (inclusive). This is due to the sine function having a range of -1 to 1.

The return value, i.e., the range of asin( ) is from -π/2 to π/2 (in radians).

Input data types

The asin() function can be provided inputs of the following types:

  • int
  • double
  • float
  • long double

The return data type for the aforementioned input data types are:

double asin(double x)
float asin(float x)
double asin(int x)
long double asin(long double)

Code

Example 1: x within range (between 1 and -1)

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = 0.5;
double answer = asin(x);
cout << "The answer in radians is: " << answer << endl;
cout << "The answer in degrees is: " << (answer * 180)/3.142 << endl; //converting radians to degrees
return 0;
}

When the parameter value is within the specified domain, the code executes normally.

Example 2: x out of range (greater than 1 or less than -1)

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double x = -2;
double answer = asin(x);
cout << "The answer in radians is: " << answer << endl;
cout << "The answer in degrees is: " << (answer * 180)/3.142 << endl; //converting radians to degrees
return 0;
}

When x is not between -1 and 1, the output is nan, i.e., Not a Number. This occurs due to a domain error, where the input value is invalid because it is beyond the allowed values of a function. This results in an undefined expression.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved