Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c++

How to use asin() in C++

Maham Ghazanfar

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)
asin() prototype for different data types

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;
}
parameter in domain

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;
}
parameter not in domain

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.

RELATED TAGS

c++

CONTRIBUTOR

Maham Ghazanfar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring