I need to implement experience filter like this
- 0 to 2 years
- 2+ to 4 years
How to express it in swift range?
Problem is I can't express more than 2 to 4 years. While I can do less than upper bounds. e.g. like this
let underTen = 0.0..<10.0
I need something like this (greater than lower bound)
let uptoTwo = 0.0...2.0
let twoPlus = 2.0>..4.0 // compiler error
Currently I am doing
let twoPlus = 2.1...4.0
But this is not perfect.
nextUp
from theFloatingPoint
protocolYou can make use of the
nextUp
property ofDouble
, as blueprinted in theFloatingPoint
protocol to whichDouble
conformsI.e.:
The property
ulp
, also blueprinted in theFloatingPoint
protocol, has been mentioned in the comments to your question. For most numbers, this is the difference betweenself
and the next greater representable number:nextUp
does, in essence, return the value ofself
with the addition ofulp
. So for your example above, the following is equivalent (whereas, imo,nextup
should be preferred in this use case).You might also want to consider replacing the lower bound literal in
twoPlus
with theupperBound
property of the precedinguptoTwo
range: