Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add func & test for getting numerator & denominator of fixed point decimal number #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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