C library functions -asin()

C standard library - <math.h>

Description

asin()is part of the C standard library<math.h>A function used to calculate the arcsine of a number (also known as the inverse sine function among the inverse trigonometric functions). This function accepts a real number between -1 and 1 and returns the arcsine of that value, in radians.

Declaration

Below is the declaration of the asin() function:

#include <math.h>

double asin(double x);
float asinf(float x);
long double asinl(long double x);

Parameter

  • x-- A floating-point number between -1 and 1.

Return Value

  • returnxthe arcsine of the value, in radians. The return value ranges from [-π/2, π/2].
  • ifxIf not in the range [-1, 1], the function will return NaN and set an appropriate math error (e.g.EDOM)。

Example

The following example demonstrates the usage of the asin() function.

Example

#include <stdio.h>
#include <math.h>

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<math.h>and<stdio.h>。
  • define a variablex, the value is 0.5.
  • Usageasin()Computexthe arcsine of, and stores the result inresultIn.
  • printxthe arcsine value, in radians.
  • Convert the result to degrees and print it out. The conversion formula is弧度 * (180 / π)。

Let us 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 inputxis outside the valid range [-1, 1],asin()will return NaN and seterrnoisEDOM. You can useisnan()The function checks whether the result is NaN:

Example

#include <stdio.h>
#include <math.h>
#include <errno.h>

int main() {
    double x = 2.0;  // Invalid input, out of [-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 attempts to computeasin(2.0)this is an invalid input because 2.0 is not in the range [-1, 1].
  • errnois reset to 0, then callasin(x)。
  • CheckserrnoWhether it equalsEDOMIf so, print the error message.
  • If there is no error, it prints the computed result.

Let us compile and run the above program, which will produce the following result:

asin(2.000000) = nan radians

asin()The function is used to compute the arcsine of a given value and is a common tool for handling inverse trigonometric operations. When using it, you should ensure that the input value is in the range [-1, 1] and handle possible error conditions. By converting the result to degrees, you can understand the result more intuitively.

C standard library - <math.h>

other extensions