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

do not add methods to promote_type #4

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/TinyHugeNumbers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TinyHugeNumbers

export tiny, huge
export tiny, huge

import Base: show, convert, promote_rule

Expand All @@ -11,18 +11,18 @@ struct TinyNumber <: Real end

Base.show(io::IO, ::TinyNumber) = print(io, "tiny")

Base.convert(::Type{F}, ::TinyNumber) where {F <: AbstractFloat} = 10eps(F)
Base.convert(::Type{Float32}, ::TinyNumber) = 1.0f-6
Base.convert(::Type{Float64}, ::TinyNumber) = 1e-12
Base.convert(::Type{F}, ::TinyNumber) where {F<:AbstractFloat} = 10eps(F)
Base.convert(::Type{Float32}, ::TinyNumber) = 1.0f-6
Base.convert(::Type{Float64}, ::TinyNumber) = 1e-12
Base.convert(::Type{BigFloat}, ::TinyNumber) = big"1e-24"

Base.promote_rule(::Type{TinyNumber}, ::Type{I}) where {I <: Integer} = promote_rule(TinyNumber, promote_type(I, Float64))
Base.promote_rule(::Type{TinyNumber}, ::Type{F}) where {F <: AbstractFloat} = F
Base.promote_rule(::Type{TinyNumber}, ::Type{I}) where {I<:Integer} = promote_type(TinyNumber, promote_type(I, Float64))
Base.promote_rule(::Type{TinyNumber}, ::Type{F}) where {F<:AbstractFloat} = F

(::Type{T})(::TinyNumber) where {T <: AbstractFloat} = convert(T, tiny)
(::Type{T})(::TinyNumber) where {T<:AbstractFloat} = convert(T, tiny)

(::TinyNumber)(::Type{ T }) where {T} = convert(T, TinyNumber())
(::TinyNumber)(something) = convert(typeof(something), TinyNumber())
(::TinyNumber)(::Type{T}) where {T} = convert(T, TinyNumber())
(::TinyNumber)(something) = convert(typeof(something), TinyNumber())

"""
tiny
Expand Down Expand Up @@ -69,18 +69,18 @@ struct HugeNumber <: Real end

Base.show(io::IO, ::HugeNumber) = print(io, "huge")

Base.convert(::Type{F}, ::HugeNumber) where {F <: AbstractFloat} = inv(tiny(F))
Base.convert(::Type{Float32}, ::HugeNumber) = 1.0f+6
Base.convert(::Type{Float64}, ::HugeNumber) = 1e+12
Base.convert(::Type{F}, ::HugeNumber) where {F<:AbstractFloat} = inv(tiny(F))
Base.convert(::Type{Float32}, ::HugeNumber) = 1.0f+6
Base.convert(::Type{Float64}, ::HugeNumber) = 1e+12
Base.convert(::Type{BigFloat}, ::HugeNumber) = big"1e+24"

Base.promote_rule(::Type{HugeNumber}, ::Type{I}) where {I <: Integer} = promote_rule(HugeNumber, promote_type(I, Float64))
Base.promote_rule(::Type{HugeNumber}, ::Type{F}) where {F <: AbstractFloat} = F
Base.promote_rule(::Type{HugeNumber}, ::Type{I}) where {I<:Integer} = promote_type(HugeNumber, promote_type(I, Float64))
Base.promote_rule(::Type{HugeNumber}, ::Type{F}) where {F<:AbstractFloat} = F

(::Type{T})(::HugeNumber) where {T <: AbstractFloat} = convert(T, huge)
(::Type{T})(::HugeNumber) where {T<:AbstractFloat} = convert(T, huge)

(::HugeNumber)(::Type{ T }) where {T} = convert(T, HugeNumber())
(::HugeNumber)(something) = convert(typeof(something), HugeNumber())
(::HugeNumber)(::Type{T}) where {T} = convert(T, HugeNumber())
(::HugeNumber)(something) = convert(typeof(something), HugeNumber())

"""
huge
Expand Down Expand Up @@ -120,9 +120,8 @@ const huge = HugeNumber()

## ------------------------------------------------------------------------------------ ##

Base.promote_type(::Type{T}, ::Type{TinyNumber}, ::Type{HugeNumber}) where {T} =
promote_type(promote_type(T, TinyNumber), HugeNumber)
Base.promote_type(::Type{T}, ::Type{HugeNumber}, ::Type{TinyNumber}) where {T} =
promote_type(promote_type(T, HugeNumber), TinyNumber)
Base.promote_rule(::Type{Union{TinyNumber, HugeNumber}}, ::Type{T}) where {T} = T
Base.promote_rule(::Type{TinyNumber}, ::Type{HugeNumber}) = Union{TinyNumber, HugeNumber}
Base.promote_rule(::Type{HugeNumber}, ::Type{TinyNumber}) = Union{TinyNumber, HugeNumber}

end
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ struct ArbitraryFloatType <: AbstractFloat end
@test @inferred clamp(big"1e25", tiny, huge) == big"1e+24"

for T in (Float64, Float32, BigFloat, ArbitraryFloatType)
@test promote_type(T, TinyNumber, HugeNumber) == T
@test promote_type(T, HugeNumber, TinyNumber) == T
@test @inferred(promote_type(T, TinyNumber, HugeNumber)) == T
@test @inferred(promote_type(T, HugeNumber, TinyNumber)) == T
end

for a in (1, 1.0, 0, 0.0, 1.0f0, 0.0f0, Int32(0), Int32(1), big"1", big"1.0", big"0", big"0.0")
Expand Down
Loading