-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type
NegInf
representing negative infinity (#1528)
Also expand the functionality of `PosInf` to match.
- Loading branch information
Showing
9 changed files
with
226 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,127 @@ | ||
export PosInf, inf, IntExt, is_infinite | ||
export PosInf, NegInf, inf, IntExt, is_infinite | ||
|
||
# This is a type for positive infinity for use in valuations. | ||
""" | ||
PosInf | ||
This singleton type represents positive infinity, as in: a value larger | ||
than any real number. For use in valuations and elsewhere. | ||
See [`NegInf`](@ref). | ||
""" | ||
struct PosInf | ||
end | ||
|
||
const inf = PosInf() | ||
""" | ||
NegInf | ||
+(::Int, ::PosInf) = inf | ||
This singleton type represents negative infinity, as in: a value smaller | ||
than any real number. For use in valuations and elsewhere. | ||
+(::PosInf, ::Int) = inf | ||
See [`PosInf`](@ref). | ||
""" | ||
struct NegInf | ||
end | ||
|
||
+(::PosInf, ::PosInf) = inf | ||
# type union for convenience later on | ||
const AnyInf = Union{PosInf,NegInf} | ||
|
||
-(::PosInf, ::Int) = inf | ||
# another convenience type union | ||
# TODO: maybe deprecate this one, or at least rename it; the current one seems | ||
# somewhat arbitrary now that we also have negative infinity | ||
const IntExt = Union{Int,PosInf} | ||
|
||
Base.max(::Int, ::PosInf) = inf | ||
const inf = PosInf() # TODO: for backwards compatibility; deprecate? | ||
|
||
Base.max(::PosInf, ::Int) = inf | ||
#const infinity = PosInf() # FIXME: can't have this as we already have `infinity(C::CalciumField)` | ||
|
||
Base.isless(::Int, ::PosInf) = true | ||
######################################## | ||
# | ||
# basics | ||
# | ||
######################################## | ||
|
||
Base.isless(x::Rational{Int}, ::PosInf) = denominator(x) != 0 | ||
# match the hash values of Inf and -Inf, as we also compare equal to them | ||
Base.hash(::PosInf, h::UInt) = hash(Inf, h) | ||
Base.hash(::NegInf, h::UInt) = hash(-Inf, h) | ||
|
||
Base.isless(::PosInf, ::PosInf) = false | ||
Base.show(io::IO, ::PosInf) = print(io, "infinity") # FIXME: if we can't have `infinity` as a global, maybe better print as `inf`??? | ||
Base.show(io::IO, ::NegInf) = print(io, "-infinity") | ||
|
||
Base.isless(::PosInf, ::Int) = false | ||
Base.one(::AnyInf) = 1 | ||
Base.zero(::AnyInf) = 0 | ||
|
||
Base.isless(::PosInf, ::Rational{Int}) = false | ||
######################################## | ||
|
||
Base.isfinite(::PosInf) = false | ||
Base.signbit(::PosInf) = false | ||
Base.signbit(::NegInf) = true | ||
|
||
Base.isinf(::PosInf) = true | ||
######################################## | ||
# | ||
# comparison | ||
# | ||
######################################## | ||
|
||
Base.isone(::PosInf) = false | ||
Base.:(==)(inf1::AnyInf, inf2::AnyInf) = signbit(inf1) == signbit(inf2) | ||
Base.:(==)(x::AnyInf, y::Real) = isinf(y) && signbit(y) == signbit(x) | ||
Base.:(==)(y::Real, x::AnyInf) = x == y | ||
|
||
Base.iszero(::PosInf) = false | ||
|
||
Base.one(::PosInf) = 1 | ||
Base.isless(x::Real, ::PosInf) = isfinite(x) || signbit(x) | ||
Base.isless(::PosInf, ::Real) = false | ||
|
||
Base.zero(::PosInf) = 0 | ||
Base.isless(::Real, ::NegInf) = false | ||
Base.isless(::NegInf, x::Real) = isfinite(x) || !signbit(x) | ||
|
||
Base.isless(::PosInf, ::ZZRingElem) = false | ||
Base.isless(inf1::AnyInf, inf2::AnyInf) = signbit(inf1) && !signbit(inf2) | ||
|
||
Base.isless(::ZZRingElem, ::PosInf) = true | ||
|
||
Base.isless(::PosInf, ::QQFieldElem) = false | ||
Base.isless(::PosInf, ::Union{ZZRingElem,QQFieldElem}) = false | ||
Base.isless(::Union{ZZRingElem,QQFieldElem}, ::PosInf) = true | ||
|
||
Base.isless(::QQFieldElem, ::PosInf) = true | ||
Base.isless(::NegInf, ::Union{ZZRingElem,QQFieldElem}) = true | ||
Base.isless(::Union{ZZRingElem,QQFieldElem}, ::NegInf) = false | ||
|
||
const IntExt = Union{Int,PosInf} | ||
######################################## | ||
# | ||
# other predicates | ||
# | ||
######################################## | ||
|
||
Base.isfinite(::AnyInf) = false | ||
Base.isinf(::AnyInf) = true | ||
|
||
Base.isone(::AnyInf) = false | ||
Base.iszero(::AnyInf) = false | ||
|
||
is_positive(::PosInf) = true | ||
is_positive(::NegInf) = false | ||
|
||
is_negative(::PosInf) = false | ||
is_negative(::NegInf) = true | ||
|
||
@doc raw""" | ||
is_infinite(x::Any) -> Bool | ||
Tests whether $x$ is infinite, by returning `!isfinite(x)`. | ||
Test whether $x$ is infinite. | ||
""" | ||
is_infinite(x::Any) = !isfinite(x) | ||
# TODO: should is_infinite become a synonym for `isinf` ??? | ||
|
||
######################################## | ||
# | ||
# arithmetic | ||
# | ||
######################################## | ||
|
||
# unary minus | ||
Base.:-(::PosInf) = NegInf() | ||
Base.:-(::NegInf) = inf | ||
|
||
# binary operations | ||
Base.:+(::IntegerUnion, inf::AnyInf) = inf | ||
Base.:+(inf::AnyInf, ::IntegerUnion) = inf | ||
Base.:+(inf1::AnyInf, inf2::AnyInf) = signbit(inf1) == signbit(inf2) ? inf1 : error("inf - inf is undefined") | ||
|
||
Base.:-(inf::AnyInf, ::IntegerUnion) = inf | ||
Base.:-(::IntegerUnion, inf::AnyInf) = -inf | ||
Base.:-(inf1::AnyInf, inf2::AnyInf) = inf1 + (-inf2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.