How to Find Max Value for Double and Float in Swift

How to find max value for Double and Float in Swift

As of Swift 3+, you should use:

CGFloat.greatestFiniteMagnitude
Double.greatestFiniteMagnitude
Float.greatestFiniteMagnitude

How to get max and min from set of values which contain double and integer in swift 3?

It isn't entirely clear what you are trying to do, but this is a much cleaner way of setting up your struct and finding the maximum price:

struct ListFacets {

let quantity : Int
let price : Double
}

var facetsModel = [ListFacets]()

let dict = ["0": 1, "2.1":2, "21":3, "31.5": 2, "9.45":1]

for (priceStr, quantity) in dict {
if let price = Double(priceStr) {
facetsModel.append(ListFacets(quantity: quantity, price: price))
}
}


if let maxNum = facetsModel.max( by: { (a, b) -> Bool in
return a.price < b.price
}) {
print(maxNum)
}

Max and min in an array of Float in Swift

The solution given here https://stackoverflow.com/a/24161004/1187415 works for
for all sequences of comparable elements, therefore also for an array of floats:

let floats: Array<Float> = [2.45, 7.21, 1.35, 10.22, 2.45, 3]
let numMax = maxElement(floats)

maxElement() is defined in the Swift library as

/// Returns the maximum element in `elements`.  Requires:
/// `elements` is non-empty. O(countElements(elements))
func maxElement<R : SequenceType where R.Generator.Element : Comparable>(elements: R) -> R.Generator.Element

How can i get top 2 max values from an array of float numbers in swift?

Just sort the array and take the required value

var array1 = [2.1, 2.2, 2.5, 3.0, 4.2, 2]

var array2 = array1.sort(){ $0 > $1}

//One way
let firstMax = array2[0]
let secondMax = array2[1]

//Second way
let firstMax = array2.removeFirst()
let secondMax = array2.removeFirst()

EDIT

If you want the indexes just get them like

let maxPos = array1.indexOf(firstMax)
let secMaxPos = array1.indexOf(secondMax)

If you are confused with these things just follow the normal basics as follows.

var max = -1.0, maxPos = -1, secMax = -1.0, secMaxPos = -1
for (index, value) in array1.enumerate() {
if value > max {
max = value
maxPos = index
} else if value > secMax {
secMax = value
secMaxPos = index
}
}
print("max:\(max)->pos:\(maxPos)::secMax:\(secMax)->secMaxPos:\(secMaxPos)")

Rounding a double value to x number of decimal places in swift

You can use Swift's round function to accomplish this.

To round a Double with 3 digits precision, first multiply it by 1000, round it and divide the rounded result by 1000:

let x = 1.23556789
let y = Double(round(1000 * x) / 1000)
print(y) /// 1.236

Unlike any kind of printf(...) or String(format: ...) solutions, the result of this operation is still of type Double.

EDIT:

Regarding the comments that it sometimes does not work, please read this: What Every Programmer Should Know About Floating-Point Arithmetic

Swift -Increase Float Value

Consider this solution:

extension Float {
func decimalCount() -> Int {
if self == Float(Int(self)) {
return 0
}

let integerString = String(Int(self))
let doubleString = String(self)
let decimalCount = doubleString.count - integerString.count - 1

return decimalCount
}

mutating func increaseLast(){
let powerfactor = pow(10, Float(decimalCount()))
self = (self * powerfactor + 1) / powerfactor

}
}

I didn´t test this thoroughly but it should work. Got the function decimalcount from this so answer

Usage example:

let array: [Float] = [0.999, 0.0004, 0.004, 0.009]

array.forEach{
var a = $0
a.increaseLast()
print($0)
print(a)
}

prints:

0.999
1.0
0.0004
0.0005
0.004
0.005
0.009
0.01

Find min / max value in Swift Array

Given:

let numbers = [1, 2, 3, 4, 5]

Swift 3:

numbers.min() // equals 1
numbers.max() // equals 5

Swift 2:

numbers.minElement() // equals 1
numbers.maxElement() // equals 5


Related Topics



Leave a reply



Submit