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
Advertisements