In C++, std::asin(x) is the inverse sine (also called arcsine). It undoes std::sin(): if the sine of an angle is 0.5, asin(0.5) gives back that angle, about 0.5236 radians, which is 30 degrees.
The input must be between -1 and 1, because no sine is ever outside that range. The answer is an angle in radians between -pi / 2 and pi / 2. Multiply by 180 / pi to get degrees.
std::asin() is declared in <cmath> and is part of the group Trigonometry. Trigonometric functions use radians, not degrees. To convert degrees, multiply by std::numbers::pi / 180 (C++20).
Syntax
Include <cmath> and call std::asin. Start with these lines.
C++
double a = std::asin(0.5); // double version
float b = std::asin(0.5f); // float version
Call
What it does
std::asin(0.5)
Passes a double and returns a double. Here the result is 0.523599.
std::asin(0.5f)
Passes a float, so the float overload runs and returns a float.
std::asin(1)
An integer is converted to double. Returns 1.5708 (pi / 2).
Write std::asin so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float asin(float x);
double asin(double x);
long double asin(long double x);
double asin(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. A number from -1 to 1. Anything else is outside the domain. An integer is converted to double.
Return Value
Return Value
Description
Angle
The angle in radians from -pi / 2 to pi / 2 whose sine is x.
NaN
Returned when x is below -1 or above 1. This is a domain error.
0
asin(0) is 0, and asin(-0.0) is -0.0.
C++ Examples
Example 1 — Inverse Sine of Common Values
Print asin() for a few numbers inside its range. The results are in radians.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {0.0, 0.5, 1.0, -1.0};
for (double v : values) {
std::cout << "asin(" << v << ") = " << std::asin(v) << '\n';
}
return 0;
}
asin(1.5) is NaN: 1
ratio > 1: 1
asin(ratio) is NaN: 1
clamped: 1.5708
How It Works
No angle has a sine of 1.5, so asin(1.5) is NaN and std::isnan() prints 1.
ratio is only a tiny bit above 1 (1.0000000000000002), the kind of error a long calculation can leave behind. It is still outside the range, so NaN again.
std::min(ratio, 1.0) cuts the value back to 1. Now asin() returns 1.5708. It is a good habit to clamp a computed ratio before asin().
Frequently Asked Questions
std::asin(x) returns the angle, in radians, whose sine is x. The input must be between -1 and 1, and the result is between -pi/2 and pi/2.
An angle in radians from -pi/2 to pi/2. If x is outside the range -1 to 1, the result is NaN.
From -1 to 1. Any other value returns NaN, because no angle has a sine outside that range.
No, radians. For degrees, multiply by 180 / std::numbers::pi. asin(0.5) is 0.523599 radians, or 30 degrees.
Include <cmath> and write std::asin(). That always works and picks the C++ overloads. The global name ::asin often works too, but the standard does not promise it, and it may only take a double.