Swift Double minimum()

The minimum() method returns the minimum of the given two values.

Example

// return the smallest among 10 and 20 var result = Double.minimum(10,20)
print(result) // Output: 10.0

minimum() Syntax

The syntax of the double minimum() method is:

double.minimum(firstValue, secondValue)

Here, double is an object of the Double class.


minimum() Parameters

The minimum() method takes two parameters

  • firstValue - a floating point value
  • secondValue - another floating point value

minimum() Return Values

  • returns the minimum element between firstValue and secondValue

Example 1: Swift Double minimum()

// return the smallest among 10 and 20 var result1 = Double.minimum(10,20)
print(result1)
// return the smallest among 20.8 and 25.78 var result2 = Double.minimum(20.8,25.78)
print(result2)
// return the smallest among 60.1 and 60 var result3 = Double.minimum(60,60.1)
print(result3)

Output

10.0
20.8
60.0

In the above example, we have used the minimum() method find the smallest among two provided floating point numbers.


Example 2: Swift Double and NaN (Not a Number)

// returns 10.0 var result1 = Double.minimum(10, .nan)
print(result1)
// returns nan var result2 = Double.minimum(.nan, .nan)
print(result2)

Output

10.0
nan

Here, if we pass .nan as one of the parameters, the method will return the other value.

And, if we pass .nan for both the parameters, the method will return nan.