How to check if a double value is finite in Swift

Overview

When we want to check if an object of type double is finite or infinite, we can use the Double.isFinite property.

Syntax

Double.isFinite

Return value

The value returned is a boolean value. We get true if Double is a finite value. Otherwise, we get false.

Code example

// create some double values
let d1 = 12.44
let d2 = Double.infinity
let d3 = Double.infinity / 0.4
let d4 = 0.1
// print the exponents
print(d1.isFinite)
print(d2.isFinite)
print(d3.isFinite)
print(d4.isFinite)

Explanation

  • Lines 2-5: We create some double values.
  • Lines 8-11: We check if the double values we created are finite values and we print the results to the console.

Free Resources