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

fix integer comparisons #157

Merged
merged 1 commit into from
Mar 26, 2022
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
26 changes: 16 additions & 10 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ for w in (32,64,128)
Base.convert(::Type{$Ti′}, x::$BID) = @xchk(ccall(($(bidsym(w,"to_",i′str,"_xfloor")), libbid), $Ti′, ($BID,Ref{Cuint}), x, RefArray(flags, Threads.threadid())), InexactError, :convert, $BID, x)
Base.$(Symbol("$Ti′"))(x::$BID) = convert($Ti′, x)
end

if w′ < w # integer conversion is exact
@eval Base.:(==)(dec::$BID, int::$Ti′) = dec == $BID(int)
end
end
end

Expand Down Expand Up @@ -754,16 +758,18 @@ function Base.:(==)(dec::DecimalFloatingPoint, rat::Rational)
end
end

Base.:(==)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec == T(flt, RoundUp) == T(flt, RoundDown)
Base.:>(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec > T(flt, RoundDown)
Base.:<(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec < T(flt, RoundUp)
Base.:(>=)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec >= T(flt, RoundUp)
Base.:(<=)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec <= T(flt, RoundDown)
Base.:(==)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = dec == flt
Base.:>(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = dec < flt
Base.:<(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = dec > flt
Base.:(>=)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = dec <= flt
Base.:(<=)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = dec >= flt
Base.:(==)(dec::T, flt::Union{Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec == T(flt, RoundUp) == T(flt, RoundDown)
Base.:>(dec::T, flt::Union{Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec > T(flt, RoundDown)
Base.:<(dec::T, flt::Union{Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec < T(flt, RoundUp)
Base.:(>=)(dec::T, flt::Union{Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec >= T(flt, RoundUp)
Base.:(<=)(dec::T, flt::Union{Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec <= T(flt, RoundDown)

# canonicalize comparison order:
Base.:(==)(flt::Union{Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec == flt
Base.:>(flt::Union{Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec < flt
Base.:<(flt::Union{Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec > flt
Base.:(>=)(flt::Union{Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec <= flt
Base.:(<=)(flt::Union{Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec >= flt

# used for next/prevfloat:
const pinf128 = _parse(Dec128, "+Inf")
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,15 @@ for r in (RoundUp, RoundDown, RoundNearest)
@test BigFloat(d"1.2", r) == BigFloat("1.2", r)
end

# issue #131
@test Dec32(10000000) == 10000000
@test Dec32(10000000) != 10000001
@test Dec32(10000000) <= 10000000
@test Dec32(10000000) <= 10000001
@test Dec32(10000000) < 10000001
@test Dec32(10000000) >= 10000000-1
@test Dec32(10000000) > 10000000-1

# PR #155
@test DecFP._int_maxintfloat(Dec32) === UInt32(maxintfloat(Dec32))
@test DecFP._int_maxintfloat(Dec64) === UInt64(maxintfloat(Dec64))
Expand Down