Pregunta

De acuerdo a la documentación de Apple, Swift no apoyo de las directivas de preprocesador.En C/Objective-c, el "INFINITO" de la definición es muy útil para algunas comprobaciones.Así que, ¿Cómo puedo obtener un número que nunca es menos que otro?

¿Fue útil?

Solución

Ya está construyendo el infinito y también una función de verificación.Y también podrías compararlos directamente con << / p>

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.

Otros consejos

Para valores enteros, usted debe utilizar Int.max.

var highestNumber = Int.max

//if you need negative infinity
var lowestNumber = Int.min

El uso de NSIntegerMax en lugar de Int.max o -1 * NSIntegerMax en lugar de Int.min es equivalente, pero menos bonito.(Gracias @Charlesism)

Tal vez usted puede intentarlo finite, por ejemplo,

let x:CDouble = 0.1
finite(x) // which return a CInt

para valores flotantes,

import UIKit

typealias Space = Float

var MaxSpaceSize = Space.infinity

var space:Space = 1100

space = space * 2

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top