Swift Double - isFinite Function



The isFinite property of the Double structure is used to check whether the given value is finite or not. A finite value is a value that is not infinite or has some definite value such as real numbers, integers, or rational numbers. This property considers all values as finite except NaN (not a number) and infinity. Hence, it will return false if the input value is NaN.

For example, we have a number = 10. Now using the isFinite property, we will check whether it is finite or not. So, number.isFinite will return true, which means the given number is finite.

Syntax

Following is the syntax of isFinite property −

Double.isFinite

Return Value

This property returns a boolean value. If the given value is finite, then this property will return true. Otherwise, this property will return false.

Now we will discuss the use of the isFinite property with the help of the following examples:

Example 1

Swift program to demonstrate the isFinite property. Here we will provide various inputs to check the propertys response.

import Foundation
// Double Inputs
let x1 : Double = 845.434
let x2 : Double = -3.43
let x3 : Double = 0.0
let x4 : Double = Double.infinity
let x5 : Double = Double.nan
let x6 : Double = 2.1

// Checking whether the given value is finite or not
// Using isFinite property
print("Is x1 is finite number or not?:", x1.isFinite)
print("Is x2 is finite number or not?:", x2.isFinite)
print("Is x3 is finite number or not?:", x3.isFinite)
print("Is x4 is finite number or not?:", x4.isFinite)
print("Is x5 is finite number or not?:", x5.isFinite)
print("Is x6 is finite number or not?:", x6.isFinite)

Output

Is x1 is finite number or not?: true
Is x2 is finite number or not?: true
Is x3 is finite number or not?: true
Is x4 is finite number or not?: false
Is x5 is finite number or not?: false
Is x6 is finite number or not?: true

Example 2

Swift program to check whether the given values are finite or not. Here we use the if-else condition to check whether the given expression matches the specified condition (expression1.isFinite).

import Foundation

// Input 1
var value1: Double = 234.556
var value2: Double = 2.3445

// Expression 1
var expression1 = Double((2 * value1 - (value2 / 4)) * 3)

// Checking whether the given expression returns a finite value or not
if expression1.isFinite {
   print("Yes! The given expression returns a finite value: \(expression1)")
} else {
   print("No! The given expression does not return a finite value")
}

// Input 2
var value3: Double = Double.infinity

// Expression 2
var expression2 = Double((2 * value3 - (value1 / 4)) * 3)

// Checking whether the given expression returns a finite value or not
if expression2.isFinite {
   print("Yes! The given expression returns a finite value: \(expression2)")
} else {
   print("No! The given expression does not return a finite value")
}

Output

Yes! The given expression returns a finite value: 1405.5776250000001
No! The given expression does not return a finite value

Example 3

Swift program to find the reverse of the given number. Here we first check if the given number is finite or not using isFinite. If the number is finite, then we can reverse that number using the reversed() function. Otherwise, we cannot perform the operation.

import Foundation

// Function to reverse a double number
func reverseDoubleNumber(num: Double) -> Double? {

   // Check if the value is finite
   guard num.isFinite else {
      print("Operation not possible because the entered value is not finite")
      return nil
   }

   // Convert the finite number into a string
   let stringNum = String(num)

   // Reverse the string
   let revString = String(stringNum.reversed())

   // Convert the reversed string back to a Double
   if let result = Double(revString) {
      return result
   } else {
      return nil
   }
}

let number: Double = 3456.32

if let output = reverseDoubleNumber(num: number) {
   print("Original number: \(number)")
   print("Reversed number: \(output)")
}

Output

Original number: 3456.32
Reversed number: 23.6543
Advertisements