Swift Double - minimum() Function



The minimum() function is used to calculate the lowest number between the given two numbers. This function takes two values and compares them with each other just like we do with the help of < sign, e.g., if x < y, it returns x. If both the given numbers are NaN (not a number) or any of the numbers are NaN, then this function will return NaN. This function only compares values of Double type.

For example, we have two numbers: n = 33.1 and m = 10.2. Now, using the minimum(n, m) method, we will get the minimum number between n and m, which is m = 10.2.

Syntax

Following is the syntax of the minimum() function −

func minimum(_ X: Double, _ Y: Double)

Parameters

This function takes two parameters of Double type. Here X and Y represent those numbers that we want to compare.

Return Value

This function returns the minimum number among the given two numbers.

Now we will discuss the usage of the minimum() function with the help of the following examples −

Example 1

Swift program to demonstrating the use of the minimum() function:

import Foundation

// Double Inputs
let num1 : Double = 421.1233
let num2 : Double = 1.334343
let num3 : Double = Double.nan
let num4 : Double = Double.infinity
let num5 : Double = +0.0
let num6 : Double = -123.233

// Finding minimum numbers 
// Using minimum() function
print("Minimum number: ", Double.minimum(num1, num2))
print("Minimum number: ", Double.minimum(num3, num2))
print("Minimum number: ", Double.minimum(num3, num4))
print("Minimum number: ", Double.minimum(num1, num5))
print("Minimum number: ", Double.minimum(num2, num6))

Output

Minimum number:  1.334343
Minimum number:  1.334343
Minimum number:  inf
Minimum number:  0.0
Minimum number:  -123.233

Example 2

Swift program to determine the minimum value between two numbers:

import Foundation

// Input 
let item1 : Double = 3.456
let item2 : Double = 12.345

// Finding minimum values using minimum() function
if (Double.minimum(item1, item2) == item1){
   print("Yes! \(item1) is minimum")
}else{
   print("No! \(item1) is not minimum")
}

Output

Yes! 3.456 is minimum

Example 3

Swift program to calculate minimum element from the given array. Here using the minimum() function we calculate the smallest element present in tthe given array.

import Foundation

// Function to find a minimum element from the given array
func findMinimumElement(arr: [Double]) -> Double {
   if arr.isEmpty {
      print("Array is empty")
   }

   var minItem = arr[0]
   
   // Calculating minimum element
   for y in arr {
      if Double.minimum(y, minItem) == y {
         minItem = y
      }
   }
   return minItem
}
// Input array of double type
let numbers = [4.2, 83.3, 2.3, 1.0, 532.223]
print("Minimum element:", findMinimumElement(arr: numbers))

Output

Minimum element: 1.0

Example 4

Swift program to calculate the sum of the minimum numbers from the given two arrays. Here we first calculate the minimum numbers present in the two arrays using minimum() function and then calculate their sum.

import Foundation

// Function to find the sum of the minimum element from the given arrays
func findMinimumElement(arr1: [Double], arr2: [Double]) -> Double {
   if arr1.isEmpty && arr2.isEmpty {
      print("Arrays are empty")
   }
       
   // Calculating minimum element from array 1
   var minItem1 = arr1[0]
   for y in arr1 {
      if Double.minimum(y, minItem1) == y {
         minItem1 = y
      }
   }
   
   // Calculating minimum element from array 2
   var minItem2 = arr2[0]
   for y in arr2 {
      if Double.minimum(y, minItem2) == y {
         minItem2 = y
      }
   }
   
   // Calculating the sum of minimum elements
   let result = minItem1 + minItem2
   return result
}
// Input arrays of double types
let number1 = [4.2, 83.3, 2.3, 1.0, 532.223]
let number2 = [1.2, 23.3, 34.3, 45.6, 5.6]
print("Sum of minimum elements are:", findMinimumElement(arr1: number1, arr2:number2))

Output

Sum of minimum elements are: 2.2
Advertisements