What is the asin() function in Swift?

Overview

The asin() function, also called the arc sine function, returns the inverse sine of a number.

The figure below shows the mathematical representation of the asin() function.

Mathematical representation of the inverse sine function

Note: We need to import Foundation in our code to use the asin() function. We can import it using import Foundation.

To convert radians to degrees, use the following formula:

degrees = radians * ( 180.0 / pi )

Syntax

asin(number)
//number can be real, float, or double.

Parameters

This function requires a number as a parameter. The parameter must be a value between -1 and 1. If the value is outside -1 <= parameter <= 1, then asin() returns NaN.

Return value

asin() returns the inverse sine of the number that is sent as a parameter. The return value lies in the interval [-PI/2, PI/2] radians.

Code

The code below demonstrates how to use the asin() function in Swift.

import Swift
import Foundation
//positive number in radians
print("The value of asin(0.5) :", asin(0.5), "Radians");
// negative number in radians
print("The value of asin(-0.5) :", asin(-0.5), "Radians");
//applying asin() and then converting the result in radians to degrees
// radians = 0.5
// PI = 3.14159265
print("The value of asin(0.5): ",asin(0.5) * (180.0 / Double.pi)," Degrees");
//error output
print("The value of asin(1.5): ",asin(1.5));
print("The value of asin(-1.5): ",asin(-1.5));

Explanation

  • Line 2: We add the Foundation header required for asin() function.
  • Line 5: We calculate the arc sine of the positive number using asin().
  • Line 8: We calculate the arc sine of the negative number using asin().
  • Line 13: We calculate the arc sine of a positive number using asin()and convert the result to Degrees.
  • Lines 15–17: We calculate the arc sine of exceptional numbers using asin().