Skip to content

Commit 373e002

Browse files
committed
Fix convert to Rational at the input limits
1 parent 8c3e1d9 commit 373e002

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/FixedPointDecimals.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ function convert{TI <: Integer, T, f}(::Type{TI}, x::FD{T, f})::TI
253253
div(x.i, coefficient(FD{T, f}))
254254
end
255255

256-
convert{TR<:Rational,T,f}(::Type{TR}, x::FD{T, f})::TR =
257-
x.i // T(10)^f
256+
convert{TR <: Rational, T, f}(::Type{TR}, x::FD{T, f})::TR = x.i // coefficient(FD{T, f})
258257

259258
promote_rule{T, f, TI <: Integer}(::Type{FD{T, f}}, ::Type{TI}) = FD{T, f}
260259
promote_rule{T, f, TF <: AbstractFloat}(::Type{FD{T, f}}, ::Type{TF}) = TF

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ end
114114
@test convert(BigFloat, fd) == BigInt(val) / BigInt(powt)
115115
end
116116

117+
@testset "to rational" begin
118+
fd = reinterpret(FD2, 25)
119+
@test convert(Rational, fd) == 1//4
120+
end
121+
117122
@testset "invalid" begin
118123
@test_throws InexactError convert(FD2, FD4(0.0001))
119124
@test_throws InexactError convert(FD4, typemax(FD2))
@@ -154,6 +159,13 @@ end
154159
@test convert(Float64, fd) == Float64(typemin(T) / powt)
155160
@test convert(BigFloat, fd) == BigInt(typemin(T)) / powt
156161

162+
# Converting to a rational
163+
fd = reinterpret(FD{T,f}, typemax(T))
164+
@test convert(Rational, fd) == typemax(T) // powt
165+
166+
fd = reinterpret(FD{T,f}, typemin(T))
167+
@test convert(Rational, fd) == typemin(T) // powt
168+
157169
# Adjust number of decimal places allowed so we can have `-10 < x < 10` where x is
158170
# a FD{T,f}. Only needed to test `convert(::FD, ::Integer)`
159171
f = FixedPointDecimals.max_exp10(T)

0 commit comments

Comments
 (0)