I agree with @QuinceyMorris that this should be false
. In a closed real interval, if both endpoints are the same, then the interval contains the endpoint as its only element. For example, [π, π]
contains a single element π
.
Given that (or, if) an interval isn't empty when a == b
, is it ascending or descending?
Also given that (or, if) an interval isn't empty when a == b
, how do you construct an empty interval? Is an empty interval ascending or descending?
I didn’t find any custom Equatable
conformance in your source, so I assume that in your current implementation, Interval(a, b) != Interval(b, a)
? I feel that intervals should be equal if they differ only in direction.
I think the type might be better named as ClosedRealInterva
l, since it doesn't seem to be able to model anything other than closed real intervals.
FloatingPoint
has .infinity
for positive infinity, but you might need to ban it for both a
and b
for 2 reasons: The intervals are all closed, so the endpoints can't be infinity unless you want to model intervals of extended real numbers. FloatingPoint
doesn't have a value for negative infinity, so you can only model right-unbounded intervals but not left unbounded.
semi-related:
A few months ago, I pitched a different Interval
type that intends to model all kinds of intervals (open, closed, bounded, unbounded, empty, degenerate, proper, ascending, descending) generic over almost all Comparable
types.
However, it has some significant disadvantages when it comes to type-checked correctness and performance of iteration. So, RangeExpression
types and UnboundedRange
are preferred. I've since spun it off into its own library, but haven't updated it much recently.
I don't think your Interval
has any of the disadvantages that mine does, so long as you avoid .infinity
.