Skip to content

Commit

Permalink
add func for getting numerator & denominator
Browse files Browse the repository at this point in the history
  • Loading branch information
michiboo committed Jun 28, 2020
1 parent e8c9f3b commit b72a32b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ function Base.convert(::Type{TR}, x::FD{T, f}) where {TR <: Rational, T, f}
convert(TR, x.i // coefficient(FD{T, f}))::TR
end

function Base.numerator(x::FixedDecimal{T,f})::T where {T, f}
return convert(Rational, x).num
end
function Base.denominator(x::FixedDecimal{T,f})::T where {T, f}
return convert(Rational, x).den
end

(::Type{T})(x::FD) where {T<:Union{AbstractFloat,Integer,Rational}} = convert(T, x)

Base.promote_rule(::Type{FD{T, f}}, ::Type{<:Integer}) where {T, f} = FD{T, f}
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ end
@test convert(Rational, fd) == 1//4
end

@testset "get numerator and denominator" begin
fd = reinterpret(FD2, 25)
@test denominator(fd) == 4
@test numerator(fd) == 1
end

@testset "invalid" begin
@test_throws InexactError convert(FD2, FD4(0.0001))
@test_throws InexactError convert(FD4, typemax(FD2))
Expand Down

0 comments on commit b72a32b

Please sign in to comment.