Swift Double maximum()

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

Example

// return the largest among 10 and 20 var result = Double.maximum(10,20)
print(result) // Output: 20.0

maximum() Syntax

The syntax of the double maximum() method is:

double.maximum(firstValue, secondValue)

Here, double is an object of the Double class.


maximum() Parameters

The maximum() method takes two parameters

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

maximum() Return Values

  • returns the maximum element between firstValue and secondValue

Example 1: Swift Double maximum()

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

Output

20.0
25.78
60.1

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


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

// returns 10.0 var result1 = Double.maximum(10, .nan)
print(result1)
// returns nan var result2 = Double.maximum(.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.

Did you find this article helpful?