
- Swift - Home
- Swift - Overview
- Swift - Environment
- Swift - Basic Syntax
- Swift - Variables
- Swift - Constants
- Swift - Literals
- Swift - Comments
- Swift Operators
- Swift - Operators
- Swift - Arithmetic Operators
- Swift - Comparison Operators
- Swift - Logical Operators
- Swift - Assignment Operators
- Swift - Bitwise Operators
- Swift - Misc Operators
- Swift Advanced Operators
- Swift - Operator Overloading
- Swift - Arithmetic Overflow Operators
- Swift - Identity Operators
- Swift - Range Operators
- Swift Data Types
- Swift - Data Types
- Swift - Integers
- Swift - Floating-Point Numbers
- Swift - Double
- Swift - Boolean
- Swift - Strings
- Swift - Characters
- Swift - Type Aliases
- Swift - Optionals
- Swift - Tuples
- Swift - Assertions and Precondition
- Swift Control Flow
- Swift - Decision Making
- Swift - if statement
- Swift - if...else if...else Statement
- Swift - if-else Statement
- Swift - nested if statements
- Swift - switch statement
- Swift - Loops
- Swift - for in loop
- Swift - While loop
- Swift - repeat...while loop
- Swift - continue statement
- Swift - break statement
- Swift - fall through statement
- Swift Collections
- Swift - Arrays
- Swift - Sets
- Swift - Dictionaries
- Swift Functions
- Swift - Functions
- Swift - Nested Functions
- Swift - Function Overloading
- Swift - Recursion
- Swift - Higher-Order Functions
- Swift Closures
- Swift - Closures
- Swift-Escaping and Non-escaping closure
- Swift - Auto Closures
- Swift OOps
- Swift - Enumerations
- Swift - Structures
- Swift - Classes
- Swift - Properties
- Swift - Methods
- Swift - Subscripts
- Swift - Inheritance
- Swift-Overriding
- Swift - Initialization
- Swift - Deinitialization
- Swift Advanced
- Swift - ARC Overview
- Swift - Optional Chaining
- Swift - Error handling
- Swift - Concurrency
- Swift - Type Casting
- Swift - Nested Types
- Swift - Extensions
- Swift - Protocols
- Swift - Generics
- Swift - Access Control
- Swift - Function vs Method
- Swift - SwiftyJSON
- Swift - Singleton class
- Swift Random Numbers
- Swift Opaque and Boxed Type
Swift Double - maximum() Function
The maximum() function is used to calculate the greatest number among the given numbers. It is a predefined method of Double structure. This method takes two values and compares them with each other just like we do use> sign, e.g. if x > y returns x. In this method, if both the given numbers are NaN(not a number) or any of the numbers are NaN
, then this method will return NaN
.
For example, we have two numbers: n = 10 and m = 20, now using the maximum(n, m) function we will get the maximum number between n and m which is m = 20.
Syntax
Following is the syntax of the maximum() function −
func maximum(_ X: Double, _ Y: Double)
Parameters
The function takes two Double
values (X
and Y
) to compare.
Return Value
Returns the larger of the two numbers.
Now we will discuss the usage of the maximum() function with the help of the following examples:
Example 1
Swift program to demonstrate the use of maximum()
function −
import Foundation // Double Inputs let num1 : Double = -42.32323 let num2 : Double = 23.334343 let num3 : Double = Double.nan let num4 : Double = Double.infinity let num5 : Double = 1.233 let num6 : Double = -123.233 // Finding maximum numbers // Using maximum() function print("Which is maximum: \(num1) or \(num2) = ", Double.maximum(num1, num2)) print("Which is maximum: \(num2) or \(num3) = ", Double.maximum(num2, num3)) print("Which is maximum: \(num3) or \(num4) = ", Double.maximum(num3, num4)) print("Which is maximum: \(num2) or \(num5) = ", Double.maximum(num2, num5)) print("Which is maximum: \(num1) or \(num6) = ", Double.maximum(num1, num6))
Output
Which is maximum: -42.32323 or 23.334343 = 23.334343 Which is maximum: 23.334343 or nan = 23.334343 Which is maximum: nan or inf = inf Which is maximum: 23.334343 or 1.233 = 23.334343 Which is maximum: -42.32323 or -123.233 = -42.32323
Example 2
Swift program to determine the maximum double number.
import Foundation // Input 1 let item1 : Double = 34.6565 let item2 : Double = 4.32 // Finding maximum values using maximum() function if (Double.maximum(item1, item2) == item1) { print("Yes! \(item1) is maximum") } else { print("No! \(item1) is not maximum") }
Output
Yes! 34.6565 is maximum
Example 3
Swift program to remove a maximum element from the given array. Here we find the largest element from the given array using the maximum() function and then remove it with the help of the filter() function.
import Foundation // Function to remove a maximum element from the given array func removeMaximumElement(arr: [Double]) -> [Double] { if arr.isEmpty { print("Array is empty") } var maxItem = arr[0] // Calculating maximum element for x in arr { if Double.maximum(x, maxItem) == x { maxItem = x } } // Removing the maximum element let result = arr.filter { $0 != maxItem } return result } // Input array let numbers = [4.2, 83.3, 2.3, 1.0, 532.223] print("Array before removing maximum element:", numbers) print("Array after removing maximum element:", removeMaximumElement(arr: numbers))
Output
Array before removing maximum element: [4.2, 83.3, 2.3, 1.0, 532.223] Array after removing maximum element: [4.2, 83.3, 2.3, 1.0]
Example 4
Swift program to find the maximum element from the given array and then calculate the square of that element. Here using the maximum() function we calculate the largest element present in the array after that we calculate its square using maxItem * maxItem.
import Foundation // Function to find the square of the maximum element func squareOfMaximumElement(arr: [Double]) -> Double { if arr.isEmpty { print("Array is empty") } var maxItem = arr[0] // Calculating maximum element for x in arr { if Double.maximum(x, maxItem) == x { maxItem = x } } // Square of maximum element let result = maxItem * maxItem return result } // Input array let numbers = [3.4, 12.3, 66.4, 2.1] print("Square of maximum element is:", squareOfMaximumElement(arr: numbers))
Output
Square of maximum element is: 4408.960000000001