Current learning Swift, there are ways to find max and min value for different kind of Integer like Int.max
and Int.min
Works with swift 5
public extension Double {
/// Max double value.
static var max: Double {
return Double(greatestFiniteMagnitude)
}
/// Min double value.
static var min: Double {
return Double(-greatestFiniteMagnitude)
}
}
All of them with using swift 5.
AV's answer is fine, but I find those macros hard to remember and a bit non-obvious, so eventually I made Double.MIN
and friends work:
extension Double {
static var MIN = -DBL_MAX
static var MAX_NEG = -DBL_MIN
static var MIN_POS = DBL_MIN
static var MAX = DBL_MAX
}
Don't use lowercase min and max -- those symbols are used in Swift 3.
As of Swift 3+, you should use:
CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude
Also CGFloat.infinity
, Double.infinity
or just .infinity
can be useful in such situations.
Just Write
let mxFloat = MAXFLOAT
You will get maximum value of float in swif.