-
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
9 changed files
with
141 additions
and
118 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,47 @@ | ||
Decimal(x::Decimal) = x | ||
Decimal(n::Integer) = Decimal(signbit(n), abs(n), 0) | ||
function Decimal(x::AbstractFloat) | ||
if !isfinite(x) | ||
throw(ArgumentError("$x cannot be represented as a Decimal")) | ||
end | ||
|
||
# Express `x` as a rational `u = n / 2^k`, where `k ≥ 0` | ||
u = Rational(abs(x)) | ||
|
||
# u.den = 2^k | ||
k = ndigits(u.den, base=2) - 1 | ||
|
||
# We can write | ||
# | ||
# x = n / 2^k | ||
# = n / 2^k * 10^k * 10^-k | ||
# = (n * 10^k / 2^k) * 10^-k | ||
# = (n * 5^k) * 10^-k | ||
|
||
s = signbit(x) | ||
c = u.num * BigInt(5)^k | ||
q = -k | ||
return Decimal(s, c, q) | ||
end | ||
|
||
Base.convert(::Type{Decimal}, x::Real) = Decimal(x) | ||
|
||
function Base.BigFloat(x::Decimal) | ||
y = BigFloat(x.c) | ||
if x.q ≥ 0 | ||
y *= BigTen^x.q | ||
else | ||
y /= BigTen^(-x.q) | ||
end | ||
return x.s ? -y : y | ||
end | ||
|
||
(::Type{T})(x::Decimal) where {T<:Number} = T(BigFloat(x)) | ||
|
||
# String representation of Decimal | ||
function Base.string(x::Decimal) | ||
io = IOBuffer() | ||
scientific_notation(io, x) | ||
return String(take!(io)) | ||
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,22 +1,29 @@ | ||
# Rounding | ||
function Base.round(x::Decimal; digits::Int=0, normal::Bool=false) | ||
shift = BigInt(digits) + x.q | ||
if shift > BigInt(0) || shift < x.q | ||
(normal) ? x : normalize(x, rounded=true) | ||
else | ||
c = Base.round(x.c / BigInt(10)^(-shift)) | ||
d = Decimal(x.s, BigInt(c), x.q - shift) | ||
(normal) ? d : normalize(d, rounded=true) | ||
function Base.round(x::Decimal, r::RoundingMode=RoundNearest; | ||
digits::Union{Integer,Nothing}=nothing, | ||
sigdigits::Union{Integer,Nothing}=nothing) | ||
if !isnothing(digits) && !isnothing(sigdigits) | ||
throw(ArgumentError("`round` cannot use both `digits` and `sigdigits` arguments")) | ||
end | ||
|
||
if isnothing(digits) && isnothing(sigdigits) | ||
# If neither `digits` nor `sigdigits` was specified, remove all decimal | ||
# digits | ||
digits = 0 | ||
elseif isnothing(digits) | ||
# If only `sigdigits` was specified, remove all digits besides | ||
# `sigdigits` most significant | ||
digits = -(ndigits(x.c) + x.q - sigdigits) | ||
end | ||
end | ||
|
||
function Base.trunc(x::Decimal; digits::Int=0, normal::Bool=false) | ||
shift = BigInt(digits) + x.q | ||
if shift > BigInt(0) || shift < x.q | ||
(normal) ? x : normalize(x, rounded=true) | ||
else | ||
c = Base.trunc(x.c / BigInt(10)^(-shift)) | ||
d = Decimal(x.s, BigInt(c), x.q - shift) | ||
(normal) ? d : normalize(d, rounded=true) | ||
@assert !isnothing(digits) | ||
|
||
# `-x.q` is the number of digits after the decimal place | ||
if digits ≥ -x.q | ||
return x | ||
end | ||
|
||
trun_len = -(x.q + digits) | ||
c = div(x.c, BigTen ^ trun_len, r) | ||
return Decimal(x.s, c, x.q + trun_len) | ||
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,48 @@ | ||
using Decimals | ||
using Test | ||
|
||
@testset "Rounding" begin | ||
@testset "Round" begin | ||
@test round(dec"1234.56789", digits=-5) == dec"0000.00000" | ||
@test round(dec"1234.56789", digits=-4) == dec"0000.00000" | ||
@test round(dec"1234.56789", digits=-3) == dec"1000.00000" | ||
@test round(dec"1234.56789", digits=-2) == dec"1200.00000" | ||
@test round(dec"1234.56789", digits=-1) == dec"1230.00000" | ||
@test round(dec"1234.56789", digits=0) == dec"1235.00000" | ||
@test round(dec"1234.56789", digits=1) == dec"1234.60000" | ||
@test round(dec"1234.56789", digits=2) == dec"1234.57000" | ||
@test round(dec"1234.56789", digits=3) == dec"1234.56800" | ||
@test round(dec"1234.56789", digits=4) == dec"1234.56790" | ||
@test round(dec"1234.56789", digits=5) == dec"1234.56789" | ||
@test round(dec"1234.56789", digits=6) == dec"1234.56789" | ||
|
||
@test round(Decimal(7.123456), digits=0) == dec"7" | ||
@test round(Decimal(7.123456), digits=2) == dec"7.12" | ||
@test round(Decimal(7.123456), digits=3) == dec"7.123" | ||
@test round(Decimal(7.123456), digits=5) == dec"7.12346" | ||
@test round(Decimal(7.123456), digits=6) == dec"7.123456" | ||
@test trunc(Decimal(7.123456), digits=5) == dec"7.12345" | ||
@test round(dec"1234.56789", sigdigits=0) == dec"0000.00000" | ||
@test round(dec"1234.56789", sigdigits=1) == dec"1000.00000" | ||
@test round(dec"1234.56789", sigdigits=2) == dec"1200.00000" | ||
@test round(dec"1234.56789", sigdigits=3) == dec"1230.00000" | ||
@test round(dec"1234.56789", sigdigits=4) == dec"1235.00000" | ||
@test round(dec"1234.56789", sigdigits=5) == dec"1234.60000" | ||
@test round(dec"1234.56789", sigdigits=6) == dec"1234.57000" | ||
@test round(dec"1234.56789", sigdigits=7) == dec"1234.56800" | ||
@test round(dec"1234.56789", sigdigits=8) == dec"1234.56790" | ||
@test round(dec"1234.56789", sigdigits=9) == dec"1234.56789" | ||
@test round(dec"1234.56789", sigdigits=10) == dec"1234.56789" | ||
|
||
@test round(dec"1234.56789", RoundUp, digits=-5) == dec"100000.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=-4) == dec"10000.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=-3) == dec"2000.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=-2) == dec"1300.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=-1) == dec"1240.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=0) == dec"1235.00000" | ||
@test round(dec"1234.56789", RoundUp, digits=1) == dec"1234.60000" | ||
@test round(dec"1234.56789", RoundUp, digits=2) == dec"1234.57000" | ||
@test round(dec"1234.56789", RoundUp, digits=3) == dec"1234.56800" | ||
@test round(dec"1234.56789", RoundUp, digits=4) == dec"1234.56790" | ||
@test round(dec"1234.56789", RoundUp, digits=5) == dec"1234.56789" | ||
@test round(dec"1234.56789", RoundUp, digits=6) == dec"1234.56789" | ||
|
||
@test round(Int, dec"1234.56789") === Int(1235) | ||
@test round(Int, dec"1234.56789", RoundUp) === Int(1235) | ||
@test round(Int, dec"1234.56789", RoundDown) === Int(1233) | ||
|
||
# TODO: test all rounding modes | ||
end |