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 infinitylet infValue = Double.infinity// Print the infinity valueprint(infValue)// Compare with other valueslet comp1 = infValue > 1 ? "greater" : "lesser"let comp2 = infValue < 1 ? "lesser" : "greater"let comp3 = infValue == Double.infinity ? "equal" : "lesser"// Print the resultsprint(comp1)print(comp2)print(comp3)
Explanation
- Line 2: We declare the infinity value using
Double.infinity, and store it in the variableinfValue. - Lines 8 to 10: We compare the infinity value
infValuewith some other values and infinity itself. - Lines 13 to 15: We print the results of each comparison to the console.