
- 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 - 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