Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rounding for intervals #146

Closed
omus opened this issue Oct 8, 2020 · 2 comments · Fixed by #147
Closed

Implement rounding for intervals #146

omus opened this issue Oct 8, 2020 · 2 comments · Fixed by #147

Comments

@omus
Copy link
Collaborator

omus commented Oct 8, 2020

We should implement floor, ceil, and round for intervals. Unfortunately there are some open questions around how to support rounding on intervals:

  1. Which endpoint should be rounded? Left, right, both?
  2. Should the span between the endpoints remain consistent?

Let's look at some examples of possible implementations. Let's start with rounding both endpoints:

julia> round(Interval(0.6, 1.6))  # Rounds both endpoints up, span remains the same
Interval{Float64,Closed,Closed}(1.0, 2.0)

julia> round(Interval(0.4, 1.4))  # Rounds both endpoints down, span remains the same
Interval{Float64,Closed,Closed}(0.0, 1.0)

julia> round(Interval(0.6, 1.4))  # Rounds left up and right down, span differs
Interval{Float64,Closed,Closed}(1.0, 1.0)

julia> round(Interval(0.5, 1.5))  # Note: Behaviour due to default of `RoundNearest`
Interval{Float64,Closed,Closed}(0.0, 2.0)

julia> round(Interval(1.5, 2.5))
Interval{Float64,Closed,Closed}(2.0, 2.0)

From looking at these examples rounding one endpoint up and one down definitely doesn't seem right.
Now let's take a look at some other where we floor both endpoints:

julia> floor(Interval(0.6, 1.4))  # Span was 0.8, after flooring is 1.0
Interval{Float64,Closed,Closed}(0.0, 1.0)

julia> floor(Interval(0.1, 1.9))  # Span was 1.8, after flooring is 1.0
Interval{Float64,Closed,Closed}(0.0, 1.0)

After seeing flooring both endpoints in practice I think that the span should probably remain the same after the operation.

This would mean that we should probably only perform round, floor, and ceil on a specified endpoint. Effectively, this just shifts the interval and the span always remains the same. Such an implementation also seems to work well for AnchoredInterval however we may want to have the option to round based on the anchor (which may be the left or right endpoint).

@omus
Copy link
Collaborator Author

omus commented Oct 8, 2020

I have previously made an attempt to implement rounding but ran into these problems.

@omus
Copy link
Collaborator Author

omus commented Oct 8, 2020

For unbounded intervals if you attempt to floor/ceil/round the unbounded endpoint I would expect the interval to remain unchanged. Operating on the bounded endpoint would modify the bounded endpoint but leave the unbounded endpoint as unbounded. This would be consistent with how scalar arithmetic works with intervals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant