In Swift, the acos()
function, also called the arc cosine function, returns the inverse cosine of a number.
The image below shows the mathematical representation of the acos()
function:
Note: We need to import
Foundation
in our code to use theacos()
function. We can import it usingimport Foundation
.
To convert radians to degrees, use the following formula:
degrees = radians * ( 180.0 / pi )
acos(number)
//number can be real, float, or double.
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
, acos()
returns NaN
.
This function returns the inverse cosine of a number that is sent as a parameter. The return value lies in the interval [0, PI]
radians.
import Swiftimport Foundation//positive number in radiansprint("The value of acos(0.5) :", acos(0.5), "Radians");// negative number in radiansprint("The value of acos(-0.5) :", acos(-0.5), "Radians");//applying acos() and then converting the result in radians to degrees// radians = 0.5// PI = 3.14159265print("The value of acos(0.5): ",acos(0.5) * (180.0 / Double.pi)," Degrees");//error outputprint("The value of acos(1.5): ",acos(1.5));print("The value of acos(-1.5): ",acos(-1.5));
Foundation
header required for the acos()
function.acos()
to calculate the arc cosine of the positive number.acos()
to calculate the arc cosine of the negative number.acos()
to calculate the arc cosine of a positive number using acos()
and convert the result to degrees.acos()
to calculate the arc cosine of exceptional numbers.