
- 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 - isInfinite Property
Swift provides a pre-defined property named as isInfinite. The isInfinite
is used to check whether the given value is infinite or not. Infinite or infinity (∞) is not a particular number like 2, 3, 4, etc., it is a concept that represents a boundless or endless quantity that is greater than any finite or natural number. However, in Swift, we can represent infinite value using Double.infinity
. Also, for NaN
(not a number), this property will return false
.
For example, we have a number = 3. Now using isInfinite
, we check whether it is infinite or not. So number.isInfinite
property will return false
, which means the number is not infinite. If this property returns true
, then that means the given value is infinite.
Syntax
Following is the syntax of isInfinite
property −
Double.isInfinite
Return Value
This property returns a boolean value. If the given value is infinite, then this property will return true
. Otherwise, this property will return false
.
Now we will discuss the use of the isInfinite
property with the help of the following examples:
Example 1
Swift program to demonstrate the isInfinite property. Here we will provide various inputs to the property to check its response.
import Foundation // Double Inputs let num1 : Double = 34.56 let num2 : Double = 34.43454354354353534 let num3 : Double = 0.98 let num4 : Double = Double.infinity let num5 : Double = Double.nan // Checking whether the given value is infinite or not // Using isInfinite property print("Is num1 is infinite?:", num1.isInfinite) print("Is num2 is infinite?:", num2.isInfinite) print("Is num3 is infinite?:", num3.isInfinite) print("Is num4 is infinite?:", num4.isInfinite) print("Is num5 is infinite?:", num5.isInfinite)
Output
Is num1 is infinite?: false Is num2 is infinite?: false Is num3 is infinite?: false Is num4 is infinite?: true Is num5 is infinite?: false
Example 2
Swift program to determine whether the given expressions return infinite values or not using isInfinite
property.
import Foundation // Double expressions let expression1 : Double = 12.567 * 34.2 let expression2 : Double = 34.8833783 - 2.48494 let expression3 : Double = -433.3322 let expression4 : Double = Double.infinity * 32.3 let expression5 : Double = Double.infinity / 1.2 // Checking whether the returned values of the given expressions are infinite or not // Using isInfinite property let result1 = expression1.isInfinite let result2 = expression2.isInfinite let result3 = expression3.isInfinite let result4 = expression4.isInfinite let result5 = expression5.isInfinite print("Is (\(expression1)) is infinite?:", result1) print("Is (\(expression2)) is infinite?:", result2) print("Is (\(expression3)) is infinite?:", result3) print("Is (\(expression4)) is infinite?:", result4) print("Is (\(expression5)) is infinite?:", result5)
Output
Is (429.79140000000007) is infinite?: false Is (32.398438299999995) is infinite?: false Is (-433.3322) is infinite?: false Is (inf) is infinite?: true Is (inf) is infinite?: true
Example 3
Swift program to check whether the given values are infinite or not. Here we use the if-else condition to check whether the given expression matches the specified condition (expression1.isInfinite
).
import Foundation // Input 1 // Double values var num1 : Double = 34.565667 var num2 : Double = 4.55767 // Expression var expression1 = (2 * num1 + (num2 / 4)) * 3 // Using the isInfinite property, checking whether the given expression returns infinity or not if expression1.isInfinite { print("The given expression is infinite") } else { print("The given expression return finite value : \(expression1)") } // Input 2 // Double value var num3 : Double = Double.infinity // Expression var expression2 = (2 + num1 + (num3 * 4)) + 3 // Using the isInfinite property, checking whether the given expression returns infinity or not if expression2.isInfinite { print("The given expression is infinite") } else { print("The given expression return finite value : \(expression2)") }
Output
The given expression return finite value : 210.81225449999997 The given expression is infinite
Example 4
Swift program to find the inverse of the given number. Here if the input is a finite value, then we can calculate the inverse using 1.0/num
. If the input value is infinite, then we cannot calculate the inverse.
import Foundation // Function to calculate the inverse func inverseValue(num: Double) { let inv = 1.0 / num // Checking for infinite if inv.isInfinite { print("Error: We cannot calculate the inverse because the output is infinite") } else { print("Inverse of \(num) is \(inv)") } } // Input number let inputNum = 0.0 // Calling function inverseValue(num: inputNum)
Output
Error: We cannot calculate the inverse because the output is infinite