-
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
Also expand the functionality of `PosInf` to match.
- Loading branch information
Showing
9 changed files
with
231 additions
and
79 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,131 @@ | ||
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 | ||
This singleton type represents negative infinity, as in: a value smaller | ||
than any real number. For use in valuations and elsewhere. | ||
See [`PosInf`](@ref). | ||
""" | ||
struct NegInf | ||
end | ||
|
||
# type union for convenience later on | ||
const AnyInf = Union{PosInf,NegInf} | ||
|
||
+(::Int, ::PosInf) = 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} | ||
|
||
+(::PosInf, ::Int) = inf | ||
const inf = PosInf() # TODO: for backwards compatibility; deprecate? | ||
|
||
+(::PosInf, ::PosInf) = inf | ||
#const infinity = PosInf() # FIXME: can't have this as we already have `infinity(C::CalciumField)` | ||
|
||
-(::PosInf, ::Int) = inf | ||
######################################## | ||
# | ||
# basics | ||
# | ||
######################################## | ||
|
||
Base.max(::Int, ::PosInf) = inf | ||
# 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.max(::PosInf, ::Int) = inf | ||
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(::Int, ::PosInf) = true | ||
Base.one(::AnyInf) = 1 | ||
Base.zero(::AnyInf) = 0 | ||
|
||
Base.isless(x::Rational{Int}, ::PosInf) = denominator(x) != 0 | ||
######################################## | ||
|
||
Base.isless(::PosInf, ::PosInf) = false | ||
Base.signbit(::PosInf) = false | ||
Base.signbit(::NegInf) = true | ||
|
||
Base.isless(::PosInf, ::Int) = false | ||
Base.sign(::Type{Int}, ::PosInf) = +1 | ||
Base.sign(::Type{Int}, ::NegInf) = -1 | ||
|
||
Base.isless(::PosInf, ::Rational{Int}) = false | ||
######################################## | ||
# | ||
# comparison | ||
# | ||
######################################## | ||
|
||
Base.isfinite(::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.isinf(::PosInf) = true | ||
|
||
Base.isone(::PosInf) = false | ||
Base.isless(x::Real, ::PosInf) = isfinite(x) || signbit(x) | ||
Base.isless(::PosInf, ::Real) = false | ||
|
||
Base.iszero(::PosInf) = false | ||
Base.isless(::Real, ::NegInf) = false | ||
Base.isless(::NegInf, x::Real) = isfinite(x) || !signbit(x) | ||
|
||
Base.one(::PosInf) = 1 | ||
Base.isless(inf1::AnyInf, inf2::AnyInf) = signbit(inf1) && !signbit(inf2) | ||
|
||
Base.zero(::PosInf) = 0 | ||
|
||
Base.isless(::PosInf, ::ZZRingElem) = false | ||
Base.isless(::PosInf, ::Union{ZZRingElem,QQFieldElem}) = false | ||
Base.isless(::Union{ZZRingElem,QQFieldElem}, ::PosInf) = true | ||
|
||
Base.isless(::ZZRingElem, ::PosInf) = true | ||
Base.isless(::NegInf, ::Union{ZZRingElem,QQFieldElem}) = true | ||
Base.isless(::Union{ZZRingElem,QQFieldElem}, ::NegInf) = false | ||
|
||
Base.isless(::PosInf, ::QQFieldElem) = false | ||
######################################## | ||
# | ||
# other predicates | ||
# | ||
######################################## | ||
|
||
Base.isless(::QQFieldElem, ::PosInf) = true | ||
Base.isfinite(::AnyInf) = false | ||
Base.isinf(::AnyInf) = true | ||
|
||
const IntExt = Union{Int,PosInf} | ||
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)`. | ||
""" | ||
is_infinite(x::Any) = !isfinite(x) | ||
# TODO: should is_infinite become a synonym for `isinf` ??? | ||
# TODO: this description is problematic for approximate types | ||
|
||
######################################## | ||
# | ||
# 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.