-
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
287 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,42 @@ | ||
# 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 _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 | ||
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) | ||
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 | ||
|
||
@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 | ||
|
||
# Deal with method ambiguities | ||
function Base.round(x::Decimal, ::RoundingMode{:NearestTiesAway}) | ||
return _round(x, RoundNearestTiesAway, digits=0) | ||
end | ||
function Base.round(x::Decimal, ::RoundingMode{:FromZero}) | ||
return _round(x, RoundFromZero, digits=0) | ||
end | ||
|
||
function Base.round(x::Decimal, r::RoundingMode=RoundNearest; | ||
digits::Union{Integer,Nothing}=nothing, | ||
sigdigits::Union{Integer,Nothing}=nothing) | ||
return _round(x, r, digits=digits, sigdigits=sigdigits) | ||
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.
Oops, something went wrong.