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 Irrational and BigFloat comparisons #158

Merged
merged 1 commit into from
Mar 28, 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
Fix Irrational and BigFloat comparisons
  • Loading branch information
jmkuhn committed Mar 27, 2022
commit 61e28a3faf7ee633216d194a48d223b7879b12b0
22 changes: 12 additions & 10 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ for w in (32,64,128)
```
""" $BID

@eval $BID(x::AbstractIrrational, r::RoundingMode) = $BID(string(BigFloat(x, precision=256)), r)

# fix method ambiguities:
@eval $BID(x::Rational{T}) where {T} = convert($BID, x)

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

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)
Base.:(==)(dec::T, num::Union{BigFloat,Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec == T(num, RoundUp) == T(num, RoundDown)
Base.:>(dec::T, num::Union{BigFloat,Float16,Float32,Float64,Integer,AbstractIrrational}) where {T<:DecimalFloatingPoint} = dec > T(num, RoundDown)
Base.:<(dec::T, num::Union{BigFloat,Float16,Float32,Float64,Integer,AbstractIrrational}) where {T<:DecimalFloatingPoint} = dec < T(num, RoundUp)
Base.:(>=)(dec::T, num::Union{BigFloat,Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec >= T(num, RoundUp)
Base.:(<=)(dec::T, num::Union{BigFloat,Float16,Float32,Float64,Integer}) where {T<:DecimalFloatingPoint} = dec <= T(num, RoundDown)
stevengj marked this conversation as resolved.
Show resolved Hide resolved

# 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
Base.:(==)(num::Union{BigFloat,Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec == num
Base.:>(num::Union{BigFloat,Float16,Float32,Float64,Integer,AbstractIrrational}, dec::T) where {T<:DecimalFloatingPoint} = dec < num
Base.:<(num::Union{BigFloat,Float16,Float32,Float64,Integer,AbstractIrrational}, dec::T) where {T<:DecimalFloatingPoint} = dec > num
Base.:(>=)(num::Union{BigFloat,Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec <= num
Base.:(<=)(num::Union{BigFloat,Float16,Float32,Float64,Integer}, dec::T) where {T<:DecimalFloatingPoint} = dec >= num

# used for next/prevfloat:
const pinf128 = _parse(Dec128, "+Inf")
Expand Down
24 changes: 22 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ for T in (Dec32, Dec64, Dec128)
@test T(7) / T(100) == 7//100
@test T(7) / T(300) != 7//300

for Tf in (Float64, Float32, Float16)
for Tf in (BigFloat, Float64, Float32, Float16)
@test parse(T, "0.1") != parse(Tf, "0.1")
@test parse(Tf, "0.1") != parse(T, "0.1")
@test parse(T, "0.7") != parse(Tf, "0.7")
Expand Down Expand Up @@ -493,6 +493,26 @@ end
@test parse(Complex{Dec64}, "1.0+2.0im") == Complex(d"1.0", d"2.0")
@test parse(Dec64, SubString("1.3x", 1,3)) == d"1.3"

#issue 122
@test Dec32(π) != π
@test Dec32(π) > π
@test Dec32(π) >= π
@test π != Dec32(π)
@test π < Dec32(π)
@test π <= Dec32(π)
@test Dec64(π) != π
@test Dec64(π) < π
@test Dec64(π) <= π
@test π != Dec64(π)
@test π > Dec64(π)
@test π >= Dec64(π)
@test Dec128(π) != π
@test Dec128(π) > π
@test Dec128(π) >= π
@test π != Dec128(π)
@test π < Dec128(π)
@test π <= Dec128(π)

# issue #139
@test convert(Dec64, big"2.5") == d"2.5"

Expand Down Expand Up @@ -521,4 +541,4 @@ end
# PR #155
@test DecFP._int_maxintfloat(Dec32) === UInt32(maxintfloat(Dec32))
@test DecFP._int_maxintfloat(Dec64) === UInt64(maxintfloat(Dec64))
@test DecFP._int_maxintfloat(Dec128) === UInt128(maxintfloat(Dec128))
@test DecFP._int_maxintfloat(Dec128) === UInt128(maxintfloat(Dec128))