99

Current learning Swift, there are ways to find max and min value for different kind of Integer like Int.max and Int.min.

Is there a way to find max value for Double and Float? Moreover, which document should I refer for this kind of question? I am currently reading Apple's The Swift Programming Language.

mfaani
  • 33,269
  • 19
  • 164
  • 293
XY L
  • 25,431
  • 14
  • 84
  • 143

6 Answers6

194

As of Swift 3+, you should use:

CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude
Vadoff
  • 9,219
  • 6
  • 43
  • 39
75

While there’s no Double.max, it is defined in the C float.h header, which you can access in Swift via import Darwin.

import Darwin

let fmax = FLT_MAX
let dmax = DBL_MAX

These are roughly 3.4 * 10^38 and 1.79 * 10^308 respectively.

But bear in mind it’s not so simple with floating point numbers (it’s never simple with floating point numbers). When holding numbers this large, you lose precision in a similar way to losing precision with very small numbers, so:

let d = DBL_MAX
let e = d - 1.0
let diff = d - e
diff == 0.0  // true

let maxPlusOne = DBL_MAX + 1
maxPlusOne == d  // true

let inf = DBL_MAX * 2
// perhaps infinity is the “maximum” 
inf == Double.infinity  // true

So before you get into some calculations that might possibly brush up against these limits, you should probably read up on floating point. Here and here are probably a good start.

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118
  • I am try you code in Xcode playground and get `(+infinity)` for Double and 340,282,346,638,529,000,000,000,000,000,000,000,000.0 for Float. Why Double and Int64 both have 64bit but double can store more numbers? – XY L Feb 15 '15 at 12:27
  • 4
    You need to learn about floating point representation. Start with the [wikipedia article](http://en.wikipedia.org/wiki/Floating_point) and also [this intro](http://introcs.cs.princeton.edu/java/91float/) – Airspeed Velocity Feb 15 '15 at 12:35
  • 1
    For the common case of using max/min values with comparison operators DBL_MAX will always give valid results. – sleep Jan 18 '16 at 21:57
  • 18
    Deprecated in Swift 3, it's now `Double.greatestFiniteMagnitude` – John Montgomery Apr 20 '17 at 18:25
11

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.

AmigoNico
  • 6,652
  • 1
  • 35
  • 45
7

Just write

let mxFloat = MAXFLOAT

You will get the maximum value of a float in Swift.

shim
  • 9,289
  • 12
  • 69
  • 108
praveen kumar
  • 71
  • 1
  • 3
3

Also CGFloat.infinity, Double.infinity or just .infinity can be useful in such situations.

Renetik
  • 5,887
  • 1
  • 47
  • 66
1

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)
    }
}
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63