Swift Tutorial Swift References

Swift - fmax() Function



The Swift fmax() function returns maximum number between the two arguments.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x, lhs Specify value to compare.
y, rhs Specify value to compare.

Return Value

Returns the numerically maximum value.

Example:

In the example below, fmax() function is used to find out the maximum value between the two arguments.

import Foundation

print("fmax(50.0, 100.0) = \(fmax(50.0, 100.0))")
print("fmax(50.0, -100.0) = \(fmax(50.0, -100.0))")
print("fmax(-50.0, 50.0) = \(fmax(-50.0, 50.0))")
print("fmax(-100.0, -50.0) = \(fmax(-100.0, -50.0))")

The output of the above code will be:

fmax(50.0, 100.0) = 100.0
fmax(50.0, -100.0) = 50.0
fmax(-50.0, 50.0) = 50.0
fmax(-100.0, -50.0) = -50.0

❮ Swift Math Functions