nextUp
from the FloatingPoint
protocol
You can make use of the nextUp
property of Double
, as blueprinted in the FloatingPoint
protocol to which Double
conforms
nextUp
The least representable value that compares greater than this value.
For any finite value x
, x.nextUp
is greater than x
. ...
I.e.:
let uptoTwo = 0.0...2.0
let twoPlus = 2.0.nextUp...4.0
The property ulp
, also blueprinted in the FloatingPoint
protocol, has been mentioned in the comments to your question. For most numbers, this is the difference between self
and the next greater representable number:
ulp
The unit in the last place of self.
This is the unit of the least significant digit in the significand of
self
. For most numbers x
, this is the difference between x
and
the next greater (in magnitude) representable number. ...
nextUp
does, in essence, return the value of self
with the addition of ulp
. So for your example above, the following is equivalent (whereas, imo, nextup
should be preferred in this use case).
let uptoTwo = 0.0...2.0
let twoPlus = (2.0+2.0.ulp)...4.0
You might also want to consider replacing the lower bound literal in twoPlus
with the upperBound
property of the preceding uptoTwo
range:
let uptoTwo = 0.0...2.0 // [0, 2] closed-closed
let twoPlus = uptoTwo.upperBound.nextUp...4.0 // (2, 4] open-closed
if uptoTwo.overlaps(twoPlus) {
print("the two ranges overlap ...")
}
else {
print("ranges are non-overlapping, ok!")
}
// ranges are non-overlapping, ok!
filter
for sorting – Carboy0.0...2.0
and2.0...4.0
then it will work as desired if you check the first range and then the second range. If the value is 2.0 it will be caught by the first range. This means only values over 2.0 will be caught by the second range. – ImmenseDouble.ulp
it's as close to 2.0 as you can possibly go, without being at 2.0 – Pencelswitch years { case 0...2: print(true) case 2...4: print(true) }
– UnslingDouble.ulp
shows compiler error. – Onslaught2.0.ulp
– Unsling