What is Double.Infinity in Swift?

Overview

Double.infinity in Swift returns a positive infinity value. When compared, it is greater than all finite values and equal to infinite values.

Syntax

Double.infinity

Return value

This method returns an infinity value inf.

Code example

// Get infinity
let infValue = Double.infinity
// Print the infinity value
print(infValue)
// Compare with other values
let comp1 = infValue > 1 ? "greater" : "lesser"
let comp2 = infValue < 1 ? "lesser" : "greater"
let comp3 = infValue == Double.infinity ? "equal" : "lesser"
// Print the results
print(comp1)
print(comp2)
print(comp3)

Explanation

  • Line 2: We declare the infinity value using Double.infinity, and store it in the variable infValue.
  • Lines 8 to 10: We compare the infinity value infValue with some other values and infinity itself.
  • Lines 13 to 15: We print the results of each comparison to the console.

Free Resources