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

Empty interval as [NaN, NaN]? #408

Closed
dpsanders opened this issue Aug 21, 2020 · 13 comments · Fixed by #590
Closed

Empty interval as [NaN, NaN]? #408

dpsanders opened this issue Aug 21, 2020 · 13 comments · Fixed by #590

Comments

@dpsanders
Copy link
Member

Is there anything to stop us using Interval(NaN, NaN) as the representation of the empty interval? I think this would remove the need for a lot of isempty checks, and hence may improve performance (although this would need to be checked).

@dpsanders
Copy link
Member Author

As far as I can see, the standard mandates what inf and sup should return, but not the internal representation of the empty set.

@dpsanders
Copy link
Member Author

Indeed in section 14.2, one possible representation is given as [NaN, NaN].

@lbenet
Copy link
Member

lbenet commented Aug 21, 2020

It is indeed worth trying it. I am not so sure if we can avoid checking if an interval is empty or not, though. Maybe we should inline isempty and stick to using it.

@dpsanders
Copy link
Member Author

Julia should inline it automatically.

My idea is that e.g. for defining + of two intervals,

Interval(x.lo + y.lo, x.hi + y.hi)

should then work correctly even for empty intervals, without the explicit empty check.

@lbenet
Copy link
Member

lbenet commented Aug 21, 2020

Isn't the same true with Infs?

@dpsanders
Copy link
Member Author

Unfortunately not:

julia> add(x, y) = Interval(x.lo + y.lo, x.hi + y.hi)
add (generic function with 1 method)

julia> add(emptyinterval(Float64), entireinterval(Float64))
[NaN, NaN]

@lbenet
Copy link
Member

lbenet commented Aug 21, 2020

But...

julia> add(emptyinterval(Float64), 1..1)
∅

@dpsanders
Copy link
Member Author

Yes it works for most intervals, but not for the example I posted, so you would still need the check.

Using NaNs should work for all cases, since the NaN "poisons" the calculation.

@lbenet
Copy link
Member

lbenet commented Aug 21, 2020

You are right... Why don't you simply test your proposal?

@dpsanders
Copy link
Member Author

Yes I intend to -- I just wanted to see if there was a reason that I was missing why this is a priori a bad idea.

I think we used [NaN, NaN] at some point and then changed it!

@Kolaru
Copy link
Collaborator

Kolaru commented Aug 21, 2020

I think it would be mostly fine, but this calls for some caution about how to access bounds, since x.lo and inf(x) would not be strictly equivalent anymore.

@dpsanders
Copy link
Member Author

Yes agreed. Basically we should avoid directly accessing x.lo.

@OlivierHnt
Copy link
Member

This is interesting; it does help performance a bit. One issue is that Rational is (on master) a supported bound type for Interval, yet there is no NaN equivalent for Rational (see also issue #462).
So implementing this would mean having two distinct implementations for Rational and AbstractFloat bound types. Perhaps this is worth the extra work.

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.

4 participants