-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
@static if VERSION < v"1.10-" | ||
const libgmp = :libgmp | ||
else | ||
const libgmp = Base.GMP.libgmp | ||
end | ||
|
||
function isdivisible(x::BigInt, n::Int) | ||
r = ccall((:__gmpz_divisible_ui_p, libgmp), Cint, | ||
(Base.GMP.MPZ.mpz_t, Culong), x, n) | ||
return r != 0 | ||
end | ||
|
||
function exactdiv(x::BigInt, n::Int) | ||
y = BigInt() | ||
ccall((:__gmpz_divexact_ui, libgmp), Cvoid, | ||
(Base.GMP.MPZ.mpz_t, Base.GMP.MPZ.mpz_t, Culong), y, x, n) | ||
return y | ||
end | ||
|
||
""" | ||
maxexp(n) | ||
Return maximum exponent E such that n^E is representable both as Int and | ||
Culong (i.e., n^(E+1) would overflow). | ||
""" | ||
function maxexp(n::Int) | ||
maxval = min(typemax(Culong), typemax(Int)) | ||
return ceil(Int, log(n, maxval)) - 1 | ||
end | ||
|
||
""" | ||
cancelfactor(x::BigInt, ::Val{N}) | ||
Remove all occurrences of the factor `N` from `x`. The result is pair `(y, E)` | ||
such that `x = y * N^E`. | ||
""" | ||
function cancelfactor(x::BigInt, ::Val{N}) where {N} | ||
if iszero(x) | ||
return x, 0 | ||
end | ||
|
||
q = 0 | ||
while isdivisible(x, N) | ||
d = N | ||
q += 1 | ||
for e in 2:maxexp(N) | ||
isdivisible(x, d * N) || break | ||
d *= N | ||
q += 1 | ||
end | ||
x = exactdiv(x, d) | ||
end | ||
|
||
return x, q | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,4 +82,3 @@ end | |
|
||
# sign | ||
Base.signbit(x::Decimal) = x.s | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function Base.decompose(x::Decimal) | ||
if iszero(x) | ||
return (big(0), 0, big((-1)^x.s)) | ||
end | ||
|
||
coef = (-1)^x.s * x.c | ||
|
||
if x.q ≥ 0 | ||
return (coef * big(5)^x.q, x.q, big(1)) | ||
else | ||
coef, exp = cancelfactor(coef, Val(5)) | ||
q = -x.q - exp | ||
return (coef, x.q, big(5) ^ q) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@testset "BigInt" begin | ||
@testset "isdivisible" begin | ||
@test Decimals.isdivisible(big(12), 1) | ||
@test Decimals.isdivisible(big(12), 2) | ||
@test Decimals.isdivisible(big(12), 3) | ||
@test Decimals.isdivisible(big(12), 4) | ||
@test Decimals.isdivisible(big(12), 6) | ||
|
||
prime = big(1709) | ||
@test Decimals.isdivisible(prime, 1) | ||
@test Decimals.isdivisible(prime, Int(prime)) | ||
@test !any(n -> Decimals.isdivisible(prime, n), 2:1708) | ||
end | ||
|
||
@testset "exactdiv" begin | ||
@test Decimals.exactdiv(big(12), 2) == big(6) | ||
@test Decimals.exactdiv(big(12), 3) == big(4) | ||
@test Decimals.exactdiv(big(12), 4) == big(3) | ||
@test Decimals.exactdiv(big(12), 6) == big(2) | ||
end | ||
|
||
@testset "maxexp" begin | ||
for n in [2, 5, 10] | ||
E = Decimals.maxexp(n) | ||
@test n^E > n^(E + 1) # overflow | ||
end | ||
end | ||
|
||
@testset "cancelfactor" begin | ||
@test Decimals.cancelfactor(big(0), Val(3)) == (big(0), 0) | ||
@test Decimals.cancelfactor(big(3)^8, Val(3)) == (big(1), 8) | ||
@test Decimals.cancelfactor(big(3)^128, Val(3)) == (big(1), 128) | ||
@test Decimals.cancelfactor(big(948659)^8, Val(948659)) == (big(1), 8) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Decimals | ||
using Test | ||
|
||
@testset "Hashing" begin | ||
@testset "hash" begin | ||
@test hash(Decimal(0.)) == hash(0.) | ||
@test hash(Decimal(-0.)) == hash(-0.) | ||
@test hash(Decimal(3)) == hash(3) | ||
@test hash(Decimal(0.09375)) == hash(0.09375) | ||
@test hash(Decimal(-3)) == hash(-3) | ||
@test hash(Decimal(-0.09375)) == hash(-0.09375) | ||
|
||
# Equality implies same hash | ||
@test hash(Decimal(0, 100, 0)) == hash(Decimal(0, 10, 1)) | ||
@test hash(Decimal(0, 100, 0)) == hash(Decimal(0, 1, 2)) | ||
@test hash(Decimal(1, 100, 0)) == hash(Decimal(1, 10, 1)) | ||
@test hash(Decimal(1, 100, 0)) == hash(Decimal(1, 1, 2)) | ||
|
||
@test hash(Decimal(0, 310, -2)) == hash(Decimal(0, 31, -1)) | ||
end | ||
end |