According to Apple's documentation, Swift doesn't support preprocessor directives. In C/Objective-c the "INFINITY" definition is very useful for some checks. So, How do I get a number that never is less that another?
infinity in Swift returns a positive infinity value. When compared, it is greater than all finite values and equal to infinite values.
Infinity is a property of the global object. In other words, it is a variable in global scope. The initial value of Infinity is Number. POSITIVE_INFINITY . The value Infinity (positive infinity) is greater than any other number.
There is already buildin infinity and also a check function. And you could also directly compare them with <.
var infinity = Double.infinity var isInfinite = infinity.isInfinite var someDouble = 234432.0 if someDouble < infinity { println("Less than") } else { println("Small than") } // And the answer is Less than.
For integer values, you should use Int.max
.
var highestNumber = Int.max //if you need negative infinity var lowestNumber = Int.min
Using NSIntegerMax
instead of Int.max
or -1 * NSIntegerMax
instead of Int.min
is equivalent, but less pretty. (Thanks @Charlesism)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With