Skip to content

Commit

Permalink
Merge pull request #18 from invenia/aa/0.7
Browse files Browse the repository at this point in the history
Updates for Julia 0.7
  • Loading branch information
tinybike committed Jun 28, 2018
2 parents be5eacc + ed25bd9 commit ca06cc7
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -3,6 +3,7 @@ notifications:
email: false
julia:
- 0.6
- 0.7
- nightly
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -132,7 +132,7 @@ Inequality:

Rounding:

julia> round(decimal(3.1415), 2)
julia> round(decimal(3.1415), digits=2)
Decimal(0,314,-2)

julia> string(ans)
Expand Down
11 changes: 8 additions & 3 deletions src/Decimals.jl
Expand Up @@ -4,14 +4,19 @@
__precompile__()

module Decimals
import Base: ==, +, -, *, /, <, float, norm, inv
import Base: ==, +, -, *, /, <, float, inv, round

if VERSION < v"0.7.0-DEV.3449"
import Base: normalize
else
export normalize
end

export Decimal,
decimal,
decimal,
number

DIGITS = 20
const DIGITS = 20

# Numerical value: (-1)^s * c * 10^q
struct Decimal <: Real
Expand Down
4 changes: 2 additions & 2 deletions src/decimal.jl
Expand Up @@ -68,7 +68,7 @@ Base.one(::Type{Decimal}) = Decimal(0,1,0)

# Convert a decimal to a float
@deprecate float(x::Decimal) Float64(x)
@deprecate float(x::Array{Decimal}) map(float, x)
@deprecate float(x::Array{Decimal}) map(Float64, x)

# convert a decimal to any subtype of Real
(::Type{T})(x::Decimal) where {T<:Real} = parse(T, string(x))
Expand All @@ -83,4 +83,4 @@ end
# Check if integer
@deprecate isint(x::Integer) isinteger(x)
@deprecate isint(x::AbstractFloat) isinteger(x)
@deprecate isint(x::AbstractString) isinteger(float(x))
@deprecate isint(x::AbstractString) isinteger(parse(Float64, x))
4 changes: 2 additions & 2 deletions src/norm.jl
@@ -1,5 +1,5 @@
# Normalization: remove trailing zeros in coefficient
function Base.normalize(x::Decimal; rounded::Bool=false)
function normalize(x::Decimal; rounded::Bool=false)
p = 0
if x.c != 0
while x.c % 10^(p+1) == 0
Expand All @@ -11,7 +11,7 @@ function Base.normalize(x::Decimal; rounded::Bool=false)
if rounded
Decimal(x.s, abs(c), q)
else
round(Decimal(x.s, abs(c), q), DIGITS; normal=true)
round(Decimal(x.s, abs(c), q), digits=DIGITS, normal=true)
end
end

Expand Down
12 changes: 7 additions & 5 deletions src/round.jl
@@ -1,11 +1,13 @@
# Rounding
function Base.round(x::Decimal, dpts::Int=0; normal::Bool=false)
shift = BigInt(dpts) + x.q
function 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)
c = Base.round(x.c / BigInt(10)^(-shift))
d = Decimal(x.s, BigInt(c), x.q - shift)
(normal) ? d : normalize(d, rounded=true)
end
end

@deprecate round(x::Decimal, dpts::Int; normal::Bool=false) round(x, digits=dpts, normal=normal)
13 changes: 6 additions & 7 deletions test/test_round.jl
Expand Up @@ -3,14 +3,13 @@ using Compat.Test

@testset "Rounding" begin

@test round(Decimal(7.123456), 0) == Decimal(7)
@test round(Decimal(7.123456), 2) == Decimal(7.12)
@test round(Decimal(7.123456), 3) == Decimal(7.123)
@test round(Decimal(7.123456), 5) == Decimal(7.12346)
@test round(Decimal(7.123456), 6) == Decimal(7.123456)
@test round(Decimal(7.123456), digits=0) == Decimal(7)
@test round(Decimal(7.123456), digits=2) == Decimal(7.12)
@test round(Decimal(7.123456), digits=3) == Decimal(7.123)
@test round(Decimal(7.123456), digits=5) == Decimal(7.12346)
@test round(Decimal(7.123456), digits=6) == Decimal(7.123456)

@test round(0.123, 1) == 0.1
@test round.([0.1111 0.2222 0.8888], 2) == [0.11 0.22 0.89]
@test round.(Decimal.([0.1111, 0.2222, 0.8888]), digits=2) == Decimal.([0.11, 0.22, 0.89])

function tet()
a = parse(Decimal, "1.0000001")
Expand Down

0 comments on commit ca06cc7

Please sign in to comment.