From b72a32bccf74d7a373f52b891a00e79f17f0df11 Mon Sep 17 00:00:00 2001 From: michiboo Date: Sun, 28 Jun 2020 07:10:45 +0300 Subject: [PATCH] add func for getting numerator & denominator --- src/FixedPointDecimals.jl | 7 +++++++ test/runtests.jl | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/FixedPointDecimals.jl b/src/FixedPointDecimals.jl index 51afea0..7b8d951 100644 --- a/src/FixedPointDecimals.jl +++ b/src/FixedPointDecimals.jl @@ -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} diff --git a/test/runtests.jl b/test/runtests.jl index 33fe0ac..d4bfe05 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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))