C Programming

asin() Function in C Language (Arc sine)

The classic C language has an extensive library for solving Mathematical operations and functions. This set provides functions for operations from the simplest to the most complex.

In this Linux Hint article, we explain how to use the asin() function to find the inverse sine of a variable. In addition, we give you a tip on how to get the result in degrees.

We have prepared a section where we explain the theoretical framework of asin(), see its syntax, the input and output arguments, and the type of data each of them accepts to help you better understand how this function works.

Then, with practical examples, code fragments and pictures, we will see how to implement this function in practice, compiling the examples in gcc from the Linux command console, all explained step by step.

Syntax of asin() Function in C Language.

double asin ( double x );

Description of asin() Function in C Language.

For the following expression, the asin() function determines the arc sine or inverse sine of “x” and returns the result in “a”.

a = asin ( x );

The result returned by this function is expressed in radians and both the input argument “x” of asin() and the output argument are of type double.

For values less than -1 or greater than 1, asin returns a non-numeric value (NaN) as the result.

The asin() function is part of the C Mathematical library, so its use must be defined beforehand in our “.c” code or otherwise in the “.h” header with the following declaration.
the result in “a”.

#include <math.h>

Once the “math” library is defined in our code, we can now use the asin() function and the complementary asinl() and asinf() functions provided by the C math library.

This function is located in the “libm” library or Math library, so you should call it in the compile command with the following “-lm” command.
the result in “a”.

~$ gcc Documents/name.c -lm -o out

How to Get the arc Sine of a Variable with the asin() Function in C Language

In this example, we will see how to obtain the inverse sine of a variable using the function in the C language. For this, we will define the necessary libraries and create the variable x, which will be of type double and from which we will obtain the inverse sine.

Then, we will create the variable “a”, also of type double, which will be used to store the result.

After defining the libraries, we want to use and declaring the variables and we call the function asin(), passing “x” as the input argument and the variable “a” as the output argument.

With the printf() function, we see in the command console the result of this operation, which in this case is the arc sine of “x” or 0.5.

The following code snippet shows how to get the result in “a” and display it on the command line.
the result in “a”.

#include <stdio.h>

#include <math.h>

void main ()
  {
double x = 0.5;
double a;
   a = asin( x );
printf("The arc sine of x = %f\n", a);
  }

Once we have our sample code we compile and run as follows:

~$ gcc Documents/main.c -lm -o asin_example

~$ ./asin_example

The following figure shows the result expressed in radians for the arc sine of “x”, which in this case is 0.5.

How to Obtain the Arc Sine of a Variable in Degrees with the asin() Function in C Language.

The Mathematical functions provided by the “math” library, which include calculations related to waves and their analysis, provide results in radians. In general, the data sheets of the technical components in all their branches contain the formulas of the calculations in degrees, so it is more convenient to convert the results of these functions from radians to degrees.

Here, we show you a practical way to get the result of the function asin() in degrees. The formula for this conversion is:

Degrees = radians * (180 / 𝝿)

To get the result of the sine arc of “x” in degrees, we define a macro asin_deg in the header of our code that calls the function asin() and multiplies its result by 180 / 𝝿, as shown below.

#define asin_deg asin( x ) * ( 180 / 3.14159265359 );

Once this macro is defined, we will obtain the sine arc of “x” expressed in degrees as follows:

#include <stdio.h>

#include <math.h>

#define asin_deg asin( x ) * ( 180 / 3.14159265359 );

void main ()
  {
double x = 1;
double a;
   a = asin_deg;
printf("The arc sine in degrees of x = %f\n", a);
  }

The following image shows the result in degrees:

Conclusion

In this article, we have explained step by step how to use the asin() function in the C language to obtain the arc sine of a variable. We have also given you a practical set to get these results expressed in degrees.

We hope you found this article useful. For more tips on explaining C language, see other Linux Hint articles, which you can access through the search engine on our website.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).