Skip to content

Commit

Permalink
Comparison with Float (issue #79) (#121)
Browse files Browse the repository at this point in the history
* Comparison with Float (issue #79)

* Test 1e100

* Simplify <=, >=

* Simplify flt op dec
  • Loading branch information
jmkuhn authored May 13, 2020
1 parent 0a72392 commit 6c4ea24
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,17 @@ 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

# used for next/prevfloat:
const pinf128 = _parse(Dec128, "+Inf")
const minf128 = _parse(Dec128, "-Inf")
Expand Down
35 changes: 35 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,38 @@ 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)
@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")
@test parse(Tf, "0.7") != parse(T, "0.7")
if Tf == Float16
@test parse(T, "0.1") > parse(Tf, "0.1")
@test parse(T, "0.1") >= parse(Tf, "0.1")
@test parse(Tf, "0.1") < parse(T, "0.1")
@test parse(Tf, "0.1") <= parse(T, "0.1")
@test parse(T, "0.7") < parse(Tf, "0.7")
@test parse(T, "0.7") <= parse(Tf, "0.7")
@test parse(Tf, "0.7") > parse(T, "0.7")
@test parse(Tf, "0.7") >= parse(T, "0.7")
else
@test parse(T, "0.1") < parse(Tf, "0.1")
@test parse(T, "0.1") <= parse(Tf, "0.1")
@test parse(Tf, "0.1") > parse(T, "0.1")
@test parse(Tf, "0.1") >= parse(T, "0.1")
@test parse(T, "0.7") > parse(Tf, "0.7")
@test parse(T, "0.7") >= parse(Tf, "0.7")
@test parse(Tf, "0.7") < parse(T, "0.7")
@test parse(Tf, "0.7") <= parse(T, "0.7")
end
@test parse(T, "0.5") == parse(Tf, "0.5")
@test parse(T, "0.5") <= parse(Tf, "0.5")
@test parse(T, "0.5") >= parse(Tf, "0.5")
@test parse(Tf, "0.5") == parse(T, "0.5")
@test parse(Tf, "0.5") <= parse(T, "0.5")
@test parse(Tf, "0.5") >= parse(T, "0.5")
end

for x = -5.0:0.25:5.0, y = -5.0:0.25:5.0
@test isequal(rem(T(x), T(y)), rem(x, y))
@test isequal(rem(T(x), T(y), RoundNearest), rem(x, y, RoundNearest))
Expand Down Expand Up @@ -341,6 +373,9 @@ end

@test Float64(d64"1e100") == 1e100

# issue #79
@test d64"1e100" != 1e100

# issue #93
@test parse(Dec64, "3935767060.093896713") == d64"3.935767060093897e9" ==
Dec64(d128"3935767060.093896713")

0 comments on commit 6c4ea24

Please sign in to comment.