How to find max value for Double and Float in Swift

后端 未结 7 1623
情歌与酒
情歌与酒 2020-12-23 23:55

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

7条回答
  •  再見小時候
    2020-12-24 00:39

    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.

提交回复
热议问题