Skip to content

Commit de39990

Browse files
jmkuhnstevengj
authored andcommitted
Add round (#44)
1 parent df49cfe commit de39990

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/DecFP.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ for w in (32,64,128)
184184
Base.trunc(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xint")), libbid), $Ti′, ($BID,), x), InexactError, INVALID | OVERFLOW)
185185
Base.floor(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xfloor")), libbid), $Ti′, ($BID,), x), InexactError, INVALID | OVERFLOW)
186186
Base.ceil(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xceil")), libbid), $Ti′, ($BID,), x), InexactError, INVALID | OVERFLOW)
187+
Base.round(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xrnint")), libbid), $Ti′, ($BID,), x), InexactError, INVALID | OVERFLOW)
188+
Base.round(::Type{$Ti′}, x::$BID, ::RoundingMode{:NearestTiesAway}) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xrninta")), libbid), $Ti′, ($BID,), x), InexactError, INVALID | OVERFLOW)
187189
Base.convert(::Type{$Ti′}, x::$BID) = xchk(ccall(($(bidsym(w,"to_",lowercase(i′),"_xfloor")), libbid), $Ti′, ($BID,), x), InexactError)
188190
end
189191
end
@@ -197,8 +199,20 @@ end # widths w
197199
Base.trunc(::Type{Integer}, x::DecimalFloatingPoint) = trunc(Int, x)
198200
Base.floor(::Type{Integer}, x::DecimalFloatingPoint) = floor(Int, x)
199201
Base.ceil(::Type{Integer}, x::DecimalFloatingPoint) = ceil(Int, x)
202+
Base.round(::Type{Integer}, x::DecimalFloatingPoint) = round(Int, x)
203+
Base.round(::Type{Integer}, x::DecimalFloatingPoint, ::RoundingMode{:NearestTiesAway}) = round(Int, x, RoundNearestTiesAway)
200204
Base.convert(::Type{Integer}, x::DecimalFloatingPoint) = convert(Int, x)
201205

206+
Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:Nearest}) = round(T, x)
207+
function Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:NearestTiesUp})
208+
y = floor(T, x)
209+
ifelse(x==y, y, copysign(floor(T, 2*x-y), x))
210+
end
211+
Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:ToZero}) = trunc(T, x)
212+
Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:FromZero}) = (x>=0 ? ceil(T, x) : floor(T, x))
213+
Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:Up}) = ceil(T, x)
214+
Base.round{T<:Integer}(::Type{T}, x::DecimalFloatingPoint, ::RoundingMode{:Down}) = floor(T, x)
215+
202216
# the complex-sqrt function in base doesn't work for use, because it requires base-2 ldexp
203217
function Base.sqrt{T<:DecimalFloatingPoint}(z::Complex{T})
204218
x, y = reim(z)

test/runtests.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ for T in (Dec32, Dec64, Dec128)
8585
if Ti != Integer
8686
@test parse(T, "17") == T(Ti(17)) == Ti(17) == Ti(T(17))
8787
end
88-
@test trunc(Ti, T(4.5)) == floor(Ti, T(4.5)) == 4 == ceil(Ti, T(4.5)) - 1
88+
@test trunc(Ti, T(2.7)) === floor(Ti, T(2.7)) === round(Ti, T(2.7), RoundDown) === round(Ti, T(2.7), RoundToZero) === Ti(2)
89+
@test ceil(Ti, T(2.3)) === round(Ti, T(2.3), RoundUp) === round(Ti, T(2.3), RoundFromZero) === Ti(3)
90+
@test round(Ti, T(1.5)) === round(Ti, T(2.5)) === round(Ti, T(1.5), RoundNearest) === round(Ti, T(2.5), RoundNearest) === Ti(2)
91+
@test round(Ti, T(2.5), RoundNearestTiesAway) === round(Ti, T(3.3), RoundNearestTiesAway) === Ti(3)
92+
@test round(Ti, T(2.5), RoundNearestTiesUp) === round(Ti, T(3.3), RoundNearestTiesUp) === Ti(3)
8993
@test_throws InexactError convert(Ti, xd)
9094
@test_throws InexactError trunc(Ti, realmax(T))
9195
@test_throws InexactError floor(Ti, realmax(T))
@@ -94,7 +98,12 @@ for T in (Dec32, Dec64, Dec128)
9498
@test parse(T, "-17") == T(Ti(-17)) == Ti(-17) == Ti(T(-17))
9599
end
96100
if Ti <: Signed || Ti === Integer
97-
@test floor(Ti, T(-4.5)) == -5 == trunc(Ti, T(-4.5)) - 1 == ceil(Ti, T(-4.5)) - 1
101+
@test trunc(Ti, T(-2.7)) === round(Ti, T(-2.7), RoundToZero) === Ti(-2)
102+
@test floor(Ti, T(-2.3)) === round(Ti, T(-2.3), RoundDown) === round(Ti, T(-2.3), RoundFromZero) === Ti(-3)
103+
@test ceil(Ti, T(-2.7)) === round(Ti, T(-2.7), RoundUp) === Ti(-2)
104+
@test round(Ti, T(-1.5)) === round(Ti, T(-2.5)) === round(Ti, T(-1.5), RoundNearest) === round(Ti, T(-2.5), RoundNearest) === Ti(-2)
105+
@test round(Ti, T(-2.5), RoundNearestTiesAway) === round(Ti, T(-3.3), RoundNearestTiesAway) === Ti(-3)
106+
@test round(Ti, T(-1.5), RoundNearestTiesUp) === round(Ti, T(-0.7), RoundNearestTiesUp) === Ti(-1)
98107
@test_throws InexactError convert(Ti, yd)
99108
end
100109
end

0 commit comments

Comments
 (0)