Swift Tutorial Swift References

Swift - hypot() Function



The Swift hypot() function returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).

Syntax

In Foundation framework, it is defined as follows:

public func hypot(_ lhs: CGFloat, _ rhs: CGFloat) -> CGFloat
public func hypot(_ lhs: Float, _ rhs: Float) -> Float
public func hypot(_ lhs: Float80, _ rhs: Float80) -> Float80
public func hypot(_ __x: Double, _ __y: Double) -> Double

Parameters

x, lhs Specify a value.
y, rhs Specify a value.

Return Value

Returns positive square root of sum of squares of arguments.

Example:

In the example below, hypot() function is used to return positive square root of sum of squares of arguments.

import Foundation

print("hypot(3.0, 4.0) = \(hypot(3.0, 4.0))")
print("hypot(5.0, 12.0) = \(hypot(5.0, 12.0))")
print("hypot(8.0, 15.0) = \(hypot(8.0, 15.0))")
print("hypot(20.0, 15.0) = \(hypot(20.0, 15.0))")
print("hypot(-20.0, 15.0) = \(hypot(-20.0, 15.0))")

The output of the above code will be:

hypot(3.0, 4.0) = 5.0
hypot(5.0, 12.0) = 13.0
hypot(8.0, 15.0) = 17.0
hypot(20.0, 15.0) = 25.0
hypot(-20.0, 15.0) = 25.0

❮ Swift Math Functions

5