Swift Lang中的无限性

Dan*_*iel 20 infinity swift

根据Apple的文档,Swift不支持预处理器指令.在C/Objective-c中,"INFINITY"定义对某些检查非常有用.那么,我如何获得一个永远不会少于另一个的数字呢?

sun*_*ppy 33

已经建立无限和检查功能.你也可以直接将它们与<.

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.
Run Code Online (Sandbox Code Playgroud)


eLi*_*lie 5

对于整数值,您应该使用Int.max.

var highestNumber = Int.max

//if you need negative infinity
var lowestNumber = Int.min
Run Code Online (Sandbox Code Playgroud)

使用NSIntegerMax代替Int.max-1 * NSIntegerMax替代Int.min等效,但不太漂亮.(谢谢@Charlesism)

  • 或"Int.max",其读数优于"NSIntegerMax" (8认同)