Swift Double - isLess() Function



The isLess() function is used to determine whether the specified value is less than the given value. This method works like the less-than operator (NaN (not a number), this method will return false because NaN is not less than nor greater than any value. Also, -infinity compares less than all values except −infinity and NaN, similarly for +infinity.

For example, we have two numbers: num1 = 10.3, num2 = 32.3. Now we can compare them and find the lower number using num1.isLess(than: num2), and this function will return true.

Syntax

Following is the syntax of the isLess() function −

 func isLess(than other: Double) -> Bool 

Parameters

The other parameter represents the value that we want to compare with the given value.

Return Value

This function returns a boolean value. If the given value is less than the other, this function returns true. Otherwise, this method will return false.

Now we will discuss the usage of the isLess() function with the help of the following examples:

Example 1

Swift program to demonstrate the isLess() function. Here we will provide different types of inputs to the function to check how it behaves.

 import Foundation // Double Inputs let value1 : Double = Double.infinity let value2 : Double = 4.539202122 let value3 : Double = Double.nan let value4 : Double = -3.32323232 let value5 : Double = 10.3 // Checking smallest values // Using isLess() function print("Is \(value1) is less than \(value2)?:", value1.isLess(than: value2)) print("Is \(value2) is less than \(value3)?:", value2.isLess(than: value3)) print("Is \(value2) is less than \(value4)?:", value2.isLess(than: value4)) print("Is \(value4) is less than \(value5)?:", value4.isLess(than: value5)) print("Is \(value2) is less than \(value5)?:", value2.isLess(than: value5)) 

Output

 Is inf is less than 4.539202122?: false Is 4.539202122 is less than nan?: false Is 4.539202122 is less than -3.32323232?: false Is -3.32323232 is less than 10.3?: true Is 4.539202122 is less than 10.3?: true 

Example 2

Swift program to determine the smallest value among the given values with the help of the isLess() function.

 import Foundation // Input 1 let num1 : Double = 4.539202122 let num2 : Double = 6.32323232 // Finding smallest values using isLess() function if (num1.isLess(than: num2)){ print("Yes! \(num1) is less than \(num2)") }else{ print("No! \(num1) is not less than \(num2)") } // Input 2 let num3 : Double = 390.21289483 let num4 : Double = 10.2332 // Finding smallest values using isLess() function if (num3.isLess(than: num4)){ print("Yes! \(num3) is less than \(num4)") }else{ print("No! \(num3) is not less than \(num4)") } 

Output

 Yes! 4.539202122 is less than 6.32323232 No! 390.21289483 is not less than 10.2332 

Example 3

Swift program to find the minimum element from the given array of doubles using the isLess() function.

 import Foundation // Function to find a minimum element from the given array func findMinimumElement(arr: [Double]) -> Double { // Checking if the array is empty or not if arr.isEmpty { return 0.0 } var minimumElement = arr[0] // Finding minimum element for x in arr.dropFirst() { if x.isLess(than: minimumElement) { minimumElement = x } } return minimumElement } let inputArray = [3.4, 6.7, 1.2, 0.4] print("Minimum element is", findMinimumElement(arr: inputArray)) 

Output

 Minimum element is 0.4 
Advertisements