From 46daa3c0db4c46eaeb7d51f6228da394f085ed03 Mon Sep 17 00:00:00 2001 From: John M Kuhn Date: Sat, 17 Feb 2018 19:20:09 -0500 Subject: [PATCH 1/2] Add trunc(x), floor(x), ceil(x) --- src/DecFP.jl | 6 +++++- test/runtests.jl | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/DecFP.jl b/src/DecFP.jl index 2ef1cb6..d93db21 100644 --- a/src/DecFP.jl +++ b/src/DecFP.jl @@ -303,10 +303,12 @@ for w in (32,64,128) @eval Base.$f(x::$BID) = @xchk(ccall(($(bidsym(w,f)), libbid), $BID, ($BID,), x), DomainError, x, mask=INVALID) end - for (f,c) in ((:gamma,"tgamma"), (:-,"negate"), (:round,"nearbyint")) + for (f,c) in ((:gamma,"tgamma"), (:-,"negate"), (:trunc,"round_integral_zero"), (:floor,"round_integral_negative"), (:ceil,"round_integral_positive"), (:round,"nearbyint")) @eval Base.$f(x::$BID) = @xchk(ccall(($(bidsym(w,c)), libbid), $BID, ($BID,), x), DomainError, x, mask=INVALID) end + @eval Base.round(x::$BID, ::RoundingMode{:Nearest}) = @xchk(ccall(($(bidsym(w,"round_integral_nearest_even")), libbid), $BID, ($BID,), x), DomainError, x, mask=INVALID) + for (f,c) in ((:(==),"quiet_equal"), (:>,"quiet_greater"), (:<,"quiet_less"), (:(>=), "quiet_greater_equal"), (:(<=), "quiet_less_equal")) @eval Base.$f(x::$BID, y::$BID) = nox(ccall(($(bidsym(w,c)), libbid), Cint, ($BID,$BID), x, y) != 0) end @@ -374,6 +376,8 @@ for w in (32,64,128) @eval Base.reinterpret(::Type{$Ti}, x::$BID) = x.x end # widths w +Base.round(x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = (x>=0 ? ceil(x) : floor(x)) + Base.trunc(::Type{Integer}, x::DecimalFloatingPoint) = trunc(Int, x) Base.floor(::Type{Integer}, x::DecimalFloatingPoint) = floor(Int, x) Base.ceil(::Type{Integer}, x::DecimalFloatingPoint) = ceil(Int, x) diff --git a/test/runtests.jl b/test/runtests.jl index 5e91019..812624c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -153,6 +153,13 @@ for T in (Dec32, Dec64, Dec128) for Tf in (Float64, Float32, Float16) @test xd == Tf(x) == T(Tf(x)) == Tf(xd) end + + @test trunc(T(2.7)) === floor(T(2.7)) === round(T(2.7), RoundDown) === round(T(2.7), RoundToZero) === T(2) + @test ceil(T(2.3)) === round(T(2.3), RoundUp) === round(T(2.3), RoundFromZero) === T(3) + @test round(T(1.5)) === round(T(2.5)) === round(T(1.5), RoundNearest) === round(T(2.5), RoundNearest) === T(2) + @test round(T(2.5), RoundNearestTiesAway) === round(T(3.3), RoundNearestTiesAway) === T(3) + @test round(T(2.5), RoundNearestTiesUp) === round(T(3.3), RoundNearestTiesUp) === T(3) + for Ti in (Integer,Int8,UInt8,Int16,UInt16,Int32,UInt32,Int64,UInt64) if Ti != Integer @test parse(T, "17") == T(Ti(17)) == Ti(17) == Ti(T(17)) From a7fb5aeb885e68eb487c3ff248fb11b9d32e1fe7 Mon Sep 17 00:00:00 2001 From: John M Kuhn Date: Sat, 3 Mar 2018 11:47:51 -0500 Subject: [PATCH 2/2] Use signbit --- src/DecFP.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DecFP.jl b/src/DecFP.jl index d93db21..daeb051 100644 --- a/src/DecFP.jl +++ b/src/DecFP.jl @@ -376,7 +376,7 @@ for w in (32,64,128) @eval Base.reinterpret(::Type{$Ti}, x::$BID) = x.x end # widths w -Base.round(x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = (x>=0 ? ceil(x) : floor(x)) +Base.round(x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = signbit(x) ? floor(x) : ceil(x) Base.trunc(::Type{Integer}, x::DecimalFloatingPoint) = trunc(Int, x) Base.floor(::Type{Integer}, x::DecimalFloatingPoint) = floor(Int, x)