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

Comparison with Float (issue #79) #121

Merged
merged 4 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,17 @@ function Base.:(==)(dec::DecimalFloatingPoint, rat::Rational)
end
end

@eval Base.:(==)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec == T(flt, RoundUp) == T(flt, RoundDown)
@eval Base.:>(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec > T(flt, RoundDown)
@eval Base.:<(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec < T(flt, RoundUp)
@eval Base.:(>=)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec > T(flt, RoundDown) || dec == T(flt, RoundUp)
@eval Base.:(<=)(dec::T, flt::Union{Float16,Float32,Float64}) where {T<:DecimalFloatingPoint} = dec < T(flt, RoundUp) || dec == T(flt, RoundDown)
@eval Base.:(==)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = T(flt, RoundUp) == T(flt, RoundDown) == dec
@eval Base.:>(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = T(flt, RoundUp) > dec
@eval Base.:<(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = T(flt, RoundDown) < dec
@eval Base.:(>=)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = T(flt, RoundUp) > dec || T(flt, RoundDown) == dec
stevengj marked this conversation as resolved.
Show resolved Hide resolved
@eval Base.:(<=)(flt::Union{Float16,Float32,Float64}, dec::T) where {T<:DecimalFloatingPoint} = T(flt, RoundDown) < dec || T(flt, RoundUp) == dec

# 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 @@ -228,6 +228,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 @@ -335,6 +367,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")