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 valueslet d1 = 12.44let d2 = Double.infinitylet d3 = Double.infinity / 0.4let d4 = 0.1// print the exponentsprint(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.