-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add support for Infinity.jl #95
Changes from 6 commits
617d1e1
4e3ebe5
6b81961
52c528a
f50c266
3564a85
2e1a039
614967f
945757d
dfb91a7
f8d3231
aba26a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -71,6 +71,20 @@ struct Interval{T} <: AbstractInterval{T} | |||||||||||||||||||
end | ||||||||||||||||||||
end | ||||||||||||||||||||
|
||||||||||||||||||||
""" | ||||||||||||||||||||
Check if a value is ∞ or not. Specifically check if the type is `Infinite`, | ||||||||||||||||||||
or `InfExtended`, and then check if the value is pos or neg infinity. | ||||||||||||||||||||
""" | ||||||||||||||||||||
function isbounded(a) | ||||||||||||||||||||
T = typeof(a) | ||||||||||||||||||||
if T <: Infinite || T <: InfExtended | ||||||||||||||||||||
return !isposinf(a) && !isneginf(a) | ||||||||||||||||||||
else | ||||||||||||||||||||
return true | ||||||||||||||||||||
end | ||||||||||||||||||||
end | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
isunbounded(a) = !isbounded(a) | ||||||||||||||||||||
|
||||||||||||||||||||
Interval{T}(f, l, inc::Inclusivity) where T = Interval{T}(convert(T, f), convert(T, l), inc) | ||||||||||||||||||||
Interval{T}(f, l, x::Bool, y::Bool) where T = Interval{T}(f, l, Inclusivity(x, y)) | ||||||||||||||||||||
Interval{T}(f, l) where T = Interval{T}(f, l, true, true) | ||||||||||||||||||||
|
@@ -89,6 +103,14 @@ function Interval{T}(left::LeftEndpoint{T}, right::RightEndpoint{T}) where T | |||||||||||||||||||
Interval{T}(left.endpoint, right.endpoint, left.included, right.included) | ||||||||||||||||||||
end | ||||||||||||||||||||
|
||||||||||||||||||||
""" | ||||||||||||||||||||
When the endpoints are different types, try to find a common type to combined them into | ||||||||||||||||||||
""" | ||||||||||||||||||||
function Interval(left::LeftEndpoint{S}, right::RightEndpoint{D}) where {S, D} | ||||||||||||||||||||
T = promote_type(S, D) | ||||||||||||||||||||
return Interval{T}(left.endpoint, right.endpoint, left.included, right.included) | ||||||||||||||||||||
end | ||||||||||||||||||||
|
||||||||||||||||||||
Interval(left::LeftEndpoint{T}, right::RightEndpoint{T}) where T = Interval{T}(left, right) | ||||||||||||||||||||
|
||||||||||||||||||||
# Empty Intervals | ||||||||||||||||||||
|
@@ -161,7 +183,8 @@ end | |||||||||||||||||||
|
||||||||||||||||||||
##### ARITHMETIC ##### | ||||||||||||||||||||
|
||||||||||||||||||||
Base.:+(a::T, b) where {T <: Interval} = T(first(a) + b, last(a) + b, inclusivity(a)) | ||||||||||||||||||||
Base.:+(a::Interval, b) = Interval(first(a) + b, last(a) + b, inclusivity(a)) | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous version always kept the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This becomes the same issue as most of the other issues that cropped up by adding Infinity. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That conversion makes sense if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed by adding the conversion: cjdoris/Infinity.jl#7 |
||||||||||||||||||||
|
||||||||||||||||||||
|
||||||||||||||||||||
Base.:+(a, b::Interval) = b + a | ||||||||||||||||||||
Base.:-(a::Interval, b) = a + -b | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if a user has explicitly set
T
we shouldn't be messing with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm yeah I suppose this change is actually just a byproduct of trying to throw in unbounded values into the tests.
This test is specifically the issue (https://github.com/invenia/Intervals.jl/blob/fc/using-infinity/test/interval.jl#L46-L47):
When you create the interval on the left side of
==
it willpromote(b, a)
so if one value is∞
and the other is a value, let's say10
, then it converts both values toInfExtended
. Now on the right side of==
, ifa
is of typeInfinite
(due to being a∞
or-∞
) then it will try toconvert(Infinite, 10)
forb
, which is an error. On the other side of the spectrum, ifa
is10
which would make itInt64
type, then trying to doconvert(Int64, ∞)
forb
will also fail. I guess we can just let it be an error if somebody puts in the wrong type in the Interval constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the test should be updated to be using:
Then in the tests you can use
Interval{promote_type(typeof(a), typeof(b))}(...)