Max and min in an array of Float in Swift

后端 未结 5 840
悲哀的现实
悲哀的现实 2021-01-14 17:57

According to this answer, to obtain the maximum of an array we can do:

let nums = [1, 6, 3, 9, 4, 6];
let numMax = nums.reduce(Int.min, { max($0, $1) })
         


        
5条回答
  •  野的像风
    2021-01-14 18:49

    You can use -FLT_MAX which returns minimum magnitude of Float and used for same purpose

    let numMax = floats.reduce(-FLT_MAX, { max($0, $1) })
    

    For Double array you can use -DBL_MAX

    If you want maximum magnitude value of Float use FLT_MAX.FLT_MIN is Minimum representable postive floating-point number.

提交回复
热议问题