Skip to content

Commit

Permalink
Merge pull request #345 from willow-ahrens/wma/merge_limits
Browse files Browse the repository at this point in the history
import changes to limits
  • Loading branch information
willow-ahrens authored Dec 6, 2023
2 parents 2083d0a + 6f21a8e commit 21b4bc0
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/base/limits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,15 @@ struct Limit{T} <: Number
end

limit(x::T, s) where {T} = Limit{T}(x, s)
plus_eps(x) = limit(x, tiny_positive())
minus_eps(x) = limit(x, tiny_negative())
plus_eps(x)::Limit = limit(0, tiny_positive())
minus_eps(x)::Limit = limit(x, tiny_negative())
limit(x) = limit(x, tiny_zero())
limit(x::Limit) = x
Limit{T}(x) where {T} = limit(T(x))
drop_eps(x::Limit) = x.val
drop_eps(x::Number) = x

const Eps = plus_eps(Int8(0))
const Eps = Finch.plus_eps(Int8(0))

function Base.show(io::IO, x::Limit)
print(io, "limit(", x.val, x.sign, ")")
Expand All @@ -149,19 +151,19 @@ function Base.show(io::IO, mime::MIME"text/plain", x::Limit)
end

#Core definitions for limit type
Base.:(+)(x::Limit, y::Limit) = limit(x.val + y.val, x.sign + y.sign)
Base.:(*)(x::Limit, y::Limit) = limit(x.val * y.val, x.val * y.sign + y.val * x.sign)
Base.:(-)(x::Limit, y::Limit) = limit(x.val - y.val, x.sign - y.sign)
Base.:(<)(x::Limit, y::Limit) = x.val < y.val || (x.val == y.val && x.sign < y.sign)
Base.:(<=)(x::Limit, y::Limit) = x.val < y.val || (x.val == y.val && x.sign <= y.sign)
Base.:(==)(x::Limit, y::Limit) = x.val == y.val && x.sign == y.sign
Base.isless(x::Limit, y::Limit) = x < y
Base.:(+)(x::Limit, y::Limit)::Limit = limit(x.val + y.val, x.sign + y.sign)
Base.:(*)(x::Limit, y::Limit)::Limit = limit(x.val * y.val, x.val * y.sign + y.val * x.sign)
Base.:(-)(x::Limit, y::Limit)::Limit = limit(x.val - y.val, x.sign - y.sign)
Base.:(<)(x::Limit, y::Limit)::Bool = x.val < y.val || (x.val == y.val && x.sign < y.sign)
Base.:(<=)(x::Limit, y::Limit)::Bool = x.val < y.val || (x.val == y.val && x.sign <= y.sign)
Base.:(==)(x::Limit, y::Limit)::Bool = x.val == y.val && x.sign == y.sign
Base.isless(x::Limit, y::Limit)::Bool = x < y
Base.isinf(x::Limit) = isinf(x.val)
Base.zero(x::Limit{T}) where {T} = limit(convert(T, 0))
Base.min(x::Limit) = x
Base.max(x::Limit) = x
Base.:(+)(x::Limit) = x
Base.:(-)(x::Limit) = limit(-x.val, -x.sign)
Base.:(+)(x::Limit)::Limit = x
Base.:(-)(x::Limit)::Limit = limit(-x.val, -x.sign)

#Crazy julia multiple dispatch stuff don't worry about it
limit_types = [Int8, Int16, Int32, Int64, Int128, UInt8, UInt16, UInt32, UInt64, UInt128, BigInt, Float32, Float64]
Expand All @@ -172,6 +174,10 @@ for S in limit_types
Limit(i::$S) = Limit{$S}(i, tiny())
(::Type{$S})(i::Limit{T}) where {T} = convert($S, i.val)
Base.convert(::Type{$S}, i::Limit) = convert($S, i.val)
Base.:(+)(x::Limit, y::$S)::Limit = x + limit(y)
Base.:(+)(x::$S, y::Limit)::Limit = limit(x) + y
Base.:(-)(x::Limit, y::$S)::Limit = x - limit(y)
Base.:(-)(x::$S, y::Limit)::Limit = limit(x) - y
Base.:(<)(x::Limit, y::$S) = x < limit(y)
Base.:(<)(x::$S, y::Limit) = limit(x) < y
Base.:(<=)(x::Limit, y::$S) = x <= limit(y)
Expand All @@ -180,13 +186,13 @@ for S in limit_types
Base.:(==)(x::$S, y::Limit) = limit(x) == y
Base.isless(x::Limit, y::$S) = x < limit(y)
Base.isless(x::$S, y::Limit) = limit(x) < y
Base.max(x::$S, y::Limit) = max(limit(x), y)
Base.max(x::Limit, y::$S) = max(x, limit(y))
Base.min(x::$S, y::Limit) = min(limit(x), y)
Base.min(x::Limit, y::$S) = min(x, limit(y))
Base.max(x::$S, y::Limit)::Limit = max(limit(x), y)
Base.max(x::Limit, y::$S)::Limit = max(x, limit(y))
Base.min(x::$S, y::Limit)::Limit = min(limit(x), y)
Base.min(x::Limit, y::$S)::Limit = min(x, limit(y))
end
end

Base.promote_rule(::Type{Limit{T}}, ::Type{Limit{S}}) where {T, S} = promote_type(T, S)
Base.promote_rule(::Type{Limit{T}}, ::Type{Limit{S}}) where {T, S} = Limit(promote_type(T, S))
Base.convert(::Type{Limit{T}}, i::Limit) where {T} = Limit{T}(convert(T, i.val), i.sign)
Base.hash(x::Limit, h::UInt) = hash(typeof(x), hash(x.val, hash(x.sign, h)))

0 comments on commit 21b4bc0

Please sign in to comment.