Skip to content

Commit dab3874

Browse files
committed
Fix round(::FD) at the input limits
1 parent 21843e8 commit dab3874

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/FixedPointDecimals.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ floor{T, f}(x::FD{T, f}) = FD{T, f}(fld(x.i, coefficient(FD{T, f})))
166166

167167
# TODO: round with number of digits; should be easy
168168
function round{T, f}(x::FD{T, f}, ::RoundingMode{:Nearest}=RoundNearest)
169-
powt = T(10)^f
169+
powt = coefficient(FD{T, f})
170170
quotient, remainder = fldmod(x.i, powt)
171171
FD{T, f}(_round_to_even(quotient, remainder, powt))
172172
end

test/runtests.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,31 @@ end
435435
# to FD2
436436
@test x == round(FD2, x)
437437
end
438+
439+
@testset "limits" begin
440+
for T in CONTAINER_TYPES
441+
f = FixedPointDecimals.max_exp10(T) + 1
442+
@eval begin
443+
powt = FixedPointDecimals.coefficient(FD{$T,$f})
444+
445+
max_rounded = round($T, typemax($T) / powt)
446+
min_rounded = round($T, typemin($T) / powt)
447+
448+
# Note: all values `x` in FD{T,f} are -1 < x < 1
449+
if max_rounded > 0
450+
@test_throws InexactError round(reinterpret(FD{$T,$f}, typemax($T)))
451+
else
452+
@test round(reinterpret(FD{$T,$f}, typemax($T))) == FD{$T,$f}(max_rounded)
453+
end
454+
455+
if min_rounded < 0
456+
@test_throws InexactError round(reinterpret(FD{$T,$f}, typemin($T)))
457+
else
458+
@test round(reinterpret(FD{$T,$f}, typemin($T))) == FD{$T,$f}(min_rounded)
459+
end
460+
end
461+
end
462+
end
438463
end
439464

440465
@testset "trunc" begin

0 commit comments

Comments
 (0)