Swift Tutorial Swift References

Swift - tan() Function



The Swift tan() function returns trigonometric tangent of an angle (angle should be in radians). In the graph below, tan(x) vs x is plotted.

Tan Function

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify the angle in radian.

Return Value

Returns the trigonometric tangent of an angle.

Example:

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

import Foundation

var pi = Double.pi

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

The output of the above code will be:

tan(pi/6) = 0.5773502691896257
tan(pi/4) = 0.9999999999999999
tan(pi/3) = 1.7320508075688767

❮ Swift Math Functions

5