diff --git a/src/decimal.jl b/src/decimal.jl index 3778b4b..1cc05c2 100644 --- a/src/decimal.jl +++ b/src/decimal.jl @@ -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 diff --git a/test/test_decimal.jl b/test/test_decimal.jl index f514ede..b233af7 100644 --- a/test/test_decimal.jl +++ b/test/test_decimal.jl @@ -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))