Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/decimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ Base.signbit(x::Decimal) = x.s
Base.zero(::Type{Decimal}) = Decimal(false, 0, 0)
Base.one(::Type{Decimal}) = Decimal(false, 1, 0)

function Base.isone(x::Decimal)
if signbit(x)
return false
end

if x.q < 0
c, m = cancelfactor(x.c, Val(10), -x.q)
return isone(c) && (m == -x.q)
else
return isone(x.c) && iszero(x.q)
end
end
Base.iszero(x::Decimal) = iszero(x.c)
Base.isfinite(x::Decimal) = true
Base.isnan(x::Decimal) = false
Expand Down
7 changes: 7 additions & 0 deletions test/test_decimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ end

@test isone(one(Decimal))
@test isone(Decimal(0, 1, 0))
@test !isone(Decimal(1, 1, 0))
@test !isone(Decimal(0, 0, 1))
@test !isone(Decimal(0, 1, 1))
@test !isone(Decimal(0, 2, 0))

@test isone(Decimal(0, 1000, -3))
@test !isone(Decimal(0, 1000, -2))
@test !isone(Decimal(0, 1000, -4))

@test isfinite(Decimal(0, 1, 1))
@test !isnan(Decimal(0, 1, 1))
Expand Down
Loading