Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INFINITY in Swift Lang

Tags:

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?

like image 276
Daniel Avatar asked Jun 04 '14 03:06

Daniel


People also ask

What is infinity in Swift?

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

What is infinity in code?

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.


2 Answers

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. 
like image 55
sunkehappy Avatar answered Sep 19 '22 19:09

sunkehappy


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)

like image 37
eLillie Avatar answered Sep 19 '22 19:09

eLillie