C library function - asin()
Description
asin() is a function in the C standard library is a function used to compute the arcsine of a number (also known as the inverse sine function among inverse trigonometric functions). The function accepts a real number between -1 and 1 and returns the arcsine of the value, in radians.
Declaration
The following is the declaration of the asin() function:
#include
double asin(double x);
float asinf(float x);
long double asinl(long double x);
Parameters
- x -- A floating-point number between -1 and 1.
Return Value
- Return
xthe arcsine of x, in radians. The return value range is [-PI/2, PI/2]. - If both the
xis not in the range [-1, 1], the function returns NaN and sets an appropriate math error (such asEDOM).
Example
The following example demonstrates the use of the asin() function.
Example
#include
#include
int
main
(
)
{
double
x
=
0.5
;
double
result
=
asin
(
x
)
;
printf
(
"asin(%f) = %f radians\n"
,
x
,
result
)
;
printf
(
"asin(%f) = %f degrees\n"
,
x
,
result
180
/
M_PI
)
;
return
0
;
}
Code analysis:
- The code first includes the header file
and. - Define a variable
x, with the value 0.5. - Use
asin()Computexthe arcsine of x and stores the result inresultit. - Print
xthe arcsine of x, in radians. - Convert the result to degrees and print it. The conversion formula is
弧度 * (180 / π).
Let's compile and run the above program, which will produce the following result:
asin(0.500000) = 0.523599 radians
asin(0.500000) = 30.000000 degrees
Error handling
If the input x is outside the valid range [-1, 1],asin() will return NaN and set errno is EDOM. You can use isnan() to check whether the result is NaN:
Example
#include
#include
#include
int
main
(
)
{
double
x
=
2.0
;
// Invalid input, outside the [-1, 1] range
errno
=
0
;
// Reset errno
double
result
=
asin
(
x
)
;
if
(
errno
==
EDOM
)
{
printf
(
"Input value is out of domain. asin(%f) is not defined.\n"
,
x
)
;
}
else
{
printf
(
"asin(%f) = %f radians\n"
,
x
,
result
)
;
}
return
0
;
}
Code analysis:
- The code tries to compute
asin(2.0), which is an invalid input because 2.0 is not in the [-1, 1] range. errnois reset to 0, then callasin(x).- Checks
errnois equal toEDOM, and if so, print an error message. - If there is no error, print the computation result.
Let's compile and run the above program, which will produce the following result:
asin(2.000000) = nan radians
asin() The function computes the arcsine of a given value and is a common tool for handling inverse trigonometric operations. When using it, ensure the input value is within [-1, 1] and handle possible error conditions. By converting the result to degrees, you can understand the result more intuitively.
C Standard Library -