# Floating Point Interval

**URL:** <https://forums.swift.org/t/floating-point-interval/42263>\
**Category:** Pitches\
**Tags:** numerics\
**Created:** [November 25, 2020, 7:07am UTC](https://forums.swift.org/t/floating-point-interval/42263 "2020-11-25T07:07:35Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![wowbagger](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wowbagger/32/13847_2.png) [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Post date:** [November 25, 2020, 5:57pm UTC](https://forums.swift.org/t/floating-point-interval/42263/7 "2020-11-25T17:57:14Z")

</div>

> [@wolf](#):
>
> | | |
> | --- | --- |
> | `isEmpty` | is `a` == `b`? This is different from `ClosedRange`, which always returns `true`. |

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 `π`.

> [@wolf](#):
>
> | | |
> | --- | --- |
> | `isAscending` | is `a` \< `b`? |
> | `isDescending` | is `a` \> `b`? |

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?

> [@wolf](#):
>
> `Interval<T: FloatingPoint> : Equatable`

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.

> [@wolf](#):
>
> `Interval<T: FloatingPoint>`

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.

> [@Unify and expand Range and its likes](https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427):
>
> Some light background There currently exists 6 types in the standard library that represent intervals. 5 of them conform to RangeExpression: Range ClosedRange PartialRangeFrom PartialRangeThrough PartialRangeUpTo And a 6th UnboundedRange that is like a mystery. My beefs with ranges They do not represent all kinds of intervals. Here are the ones they are able to represent: let r1 = 0...1 // r1 is [0, 1] let r2 = 0..\<1 // r2 is [0, 1) let r3 = 0... // r3 is [0, ∞) let r4 = ...1 // r4 …

However, it has [some significant disadvantages](https://forums.swift.org/t/unify-and-expand-range-and-its-likes/38427/4) 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](https://github.com/WowbaggersLiquidLunch/Interval), 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`.

---

_[View the full topic](https://forums.swift.org/t/floating-point-interval/42263)._
