How to find max value for Double and Float in Swift

SwiftFloating PointIntDoubleInfinity

Swift Problem Overview


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.

Swift Solutions


Solution 1 - Swift

As of Swift 3+, you should use:

CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude

Solution 2 - Swift

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.

Solution 3 - Swift

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.

Solution 4 - Swift

Just Write

    let mxFloat = MAXFLOAT

You will get maximum value of float in swif.

Solution 5 - Swift

All of them with using swift 5.

enter image description here

Solution 6 - Swift

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

Solution 7 - Swift

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)
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionX.CreatesView Question on Stackoverflow
Solution 1 - SwiftVadoffView Answer on Stackoverflow
Solution 2 - SwiftAirspeed VelocityView Answer on Stackoverflow
Solution 3 - SwiftAmigoNicoView Answer on Stackoverflow
Solution 4 - Swiftpraveen kumarView Answer on Stackoverflow
Solution 5 - SwiftShady MostafaView Answer on Stackoverflow
Solution 6 - SwiftRenetikView Answer on Stackoverflow
Solution 7 - SwiftVolodymyr KulykView Answer on Stackoverflow