In Swift, I have a method that produces a number set like the following:
86.9238759555414
86.4558606813632
86.4277950105986
86.6055803862833
86.1875587264579
86.7055257286376
86.7445244949838
86.5632505027143
86.7381593407261 // This will trigger a function, because 4 consecutive numbers are within .3 range.
A number gets added to this set every second. I would like to be able to detect when 4 consecutive numbers are within .3 of each other. In the number set above, this would occur right after the last number, 86.7381593407261, because the 3 previous numbers are all within .3 of each other.
My attempt at writing in swift is below:
Properties:
var counter = 0
var maxValue = 0.0
var minValue = 0.0
Formula:
// set max - works properly.
if currentValue > maxValue {
maxValue = currentValue
}
// set min - not working. the min always prints 0.0
if currentValue < currentValue {
minValue = currentValue
}
if maxValue - currentValue <= 0.3 && minValue + currentValue <= 0.3{
//if it passes the previous 2 conditions, increment the counter and update max/min appropriately.
counter += 1
}
// If it doesn't, reset the counter to 0, and reset max and min to the Int min and max values, respectively.
else {
counter = 0
// reset max and min to the Int min and max values, respectively.
minValue = DBL_MAX
maxValue = DBL_MIN
}
if counter == 4 {
// celebrate
}