Swift Tutorial Swift References

Swift - acos() Function

The Swift acos() function returns arc cosine of a value. The return value will be in the range 0 through 𝜋.


In Foundation framework, it is defined as follows:

public func acos(_ x: CGFloat) -> CGFloat
public func acos(_ x: Float) -> Float
public func acos(_ x: Float80) -> Float80
public func acos(_ __x: Double) -> Double


x Specify the value in range [-1.0, 1.0].

Return Value

Returns the arc cosine of the value.
If the x is not in the range of [-1.0, 1.0], it returns nan.


In the example below, acos() function is used to is used to find out the arc cosine of a given value.

import Foundation

print("acos(0.0) = \(acos(0.0))")
print("acos(0.5) = \(acos(0.5))")
print("acos(1.0) = \(acos(1.0))")
print("acos(2.0) = \(acos(2.0))")

The output of the above code will be:

acos(0.0) = 1.5707963267948966
acos(0.5) = 1.0471975511965979
acos(1.0) = 0.0
acos(2.0) = nan

❮ Swift Math Functions