Swift Tutorial Swift References

Swift - cos() Function

The Swift cos() function returns trigonometric cosine of an angle (angle should be in radians). The return value will be in the range -1 through 1.

In the graph below, cos(x) vs x is plotted.

Cos Function


In Foundation framework, it is defined as follows:

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


x Specify the angle in radian.

Return Value

Returns the trigonometric cosine of an angle.


In the example below, cos() function is used to find out the trigonometric cosine of an angle.

import Foundation

var pi = Double.pi

print("cos(pi/6) = \(cos(pi/6))")
print("cos(pi/4) = \(cos(pi/4))")
print("cos(pi/3) = \(cos(pi/3))")

The output of the above code will be:

cos(pi/6) = 0.8660254037844387
cos(pi/4) = 0.7071067811865476
cos(pi/3) = 0.5000000000000001

❮ Swift Math Functions