Skip to content

Commit aff7d2b

Browse files
authored
Implement Base.isone (#103)
1 parent a29b513 commit aff7d2b

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/decimal.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ Base.signbit(x::Decimal) = x.s
33
Base.zero(::Type{Decimal}) = Decimal(false, 0, 0)
44
Base.one(::Type{Decimal}) = Decimal(false, 1, 0)
55

6+
function Base.isone(x::Decimal)
7+
if signbit(x)
8+
return false
9+
end
10+
11+
if x.q < 0
12+
c, m = cancelfactor(x.c, Val(10), -x.q)
13+
return isone(c) && (m == -x.q)
14+
else
15+
return isone(x.c) && iszero(x.q)
16+
end
17+
end
618
Base.iszero(x::Decimal) = iszero(x.c)
719
Base.isfinite(x::Decimal) = true
820
Base.isnan(x::Decimal) = false

test/test_decimal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ end
4646

4747
@test isone(one(Decimal))
4848
@test isone(Decimal(0, 1, 0))
49+
@test !isone(Decimal(1, 1, 0))
4950
@test !isone(Decimal(0, 0, 1))
51+
@test !isone(Decimal(0, 1, 1))
52+
@test !isone(Decimal(0, 2, 0))
53+
54+
@test isone(Decimal(0, 1000, -3))
55+
@test !isone(Decimal(0, 1000, -2))
56+
@test !isone(Decimal(0, 1000, -4))
5057

5158
@test isfinite(Decimal(0, 1, 1))
5259
@test !isnan(Decimal(0, 1, 1))

0 commit comments

Comments
 (0)