Skip to content

Commit 9b2bcec

Browse files
authored
Merge pull request #30 from invenia/ed/upgrade
Upgrade and fix
2 parents 18f72ce + e46326f commit 9b2bcec

16 files changed

+37
-80
lines changed

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ language: julia
22
notifications:
33
email: false
44
julia:
5-
- 0.6
65
- 0.7
76
- 1.0
7+
- 1.1
88
- nightly
9-
script:
9+
after_success:
1010
- |
1111
julia -e '
12-
VERSION >= v"0.7.0-DEV.3656" && using Pkg
13-
VERSION >= v"0.7.0-DEV.5183" || cd(Pkg.dir("Decimals"))
12+
using Pkg
1413
Pkg.add("Coverage")
1514
using Coverage
1615
Coveralls.submit(Coveralls.process_folder())

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
julia 0.6
2-
Compat 0.33
1+
julia 0.7

src/Decimals.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
# Pure Julia decimal arithmetic
22
# @license MIT
33
# @author [email protected] (Jack Peterson), 7/3/2014
4-
VERSION < v"0.7.0-beta2.199" && __precompile__()
54

65
module Decimals
76
import Base: ==, +, -, *, /, <, float, inv, round
87

9-
if VERSION < v"0.7.0-DEV.3449"
10-
import Base: normalize
11-
else
12-
export normalize
13-
end
14-
158
export Decimal,
169
decimal,
17-
number
10+
number,
11+
normalize
1812

1913
const DIGITS = 20
2014

2115
# Numerical value: (-1)^s * c * 10^q
22-
struct Decimal <: Real
16+
struct Decimal <: AbstractFloat
2317
s::Integer # sign can be 0 (+) or 1 (-)
2418
c::BigInt # coefficient (significand), must be non-negative
2519
q::Integer # exponent

src/arithmetic.jl

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,12 @@ function +(x::Decimal, y::Decimal)
1515
c = BigInt(cx) + BigInt(cy)
1616
normalize(Decimal(s, abs(c), min(x.q, y.q)))
1717
end
18-
@deprecate (+)(x::Number, y::Array{Decimal}) x .+ y
19-
@deprecate (+)(x::Array{Decimal}, y::Number) x .+ y
20-
@deprecate (+)(x::Array{<:Number}, y::Array{Decimal}) x .+ y
21-
@deprecate (+)(x::Array{Decimal}, y::Array{<:Number}) x .+ y
2218

2319
# Negation
2420
-(x::Decimal) = Decimal((x.s == 1) ? 0 : 1, x.c, x.q)
2521

2622
# Subtraction
2723
-(x::Decimal, y::Decimal) = +(x, -y)
28-
@deprecate (-)(x::Number, y::Array{Decimal}) x .- y
29-
@deprecate (-)(x::Array{Decimal}, y::Number) x .- y
30-
@deprecate (-)(x::Array{<:Number}, y::Array{Decimal}) x .- y
31-
@deprecate (-)(x::Array{Decimal}, y::Array{<:Number}) x .- y
3224

3325
# Multiplication
3426
function *(x::Decimal, y::Decimal)
@@ -47,7 +39,6 @@ function Base.inv(x::Decimal)
4739
q = (x.q < 0) ? 1 - b - DIGITS : -b - DIGITS
4840
normalize(Decimal(x.s, c, q))
4941
end
50-
@deprecate inv(x::Array{Decimal}) map(inv, x)
5142

5243
# Division
5344
/(x::Decimal, y::Decimal) = x * inv(y)

src/decimal.jl

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ end
99

1010
decimal(str::AbstractString) = parse(Decimal, str)
1111

12-
# convert an array to an array of decimals
13-
@deprecate decimal(x::Array) map(decimal, x)
14-
1512
# Convert a number to a decimal
1613
Decimal(num::Real) = parse(Decimal, string(num))
1714
Base.convert(::Type{Decimal}, num::Real) = Decimal(num::Real)
@@ -66,10 +63,6 @@ end
6663
Base.zero(::Type{Decimal}) = Decimal(0,0,0)
6764
Base.one(::Type{Decimal}) = Decimal(0,1,0)
6865

69-
# Convert a decimal to a float
70-
@deprecate float(x::Decimal) Float64(x)
71-
@deprecate float(x::Array{Decimal}) map(Float64, x)
72-
7366
# convert a decimal to any subtype of Real
7467
(::Type{T})(x::Decimal) where {T<:Real} = parse(T, string(x))
7568

@@ -78,9 +71,6 @@ function number(x::Decimal)
7871
ix = (str = string(x) ; fx = parse(Float64, str); round(Int64, fx))
7972
(ix == fx) ? ix : fx
8073
end
81-
@deprecate number(x::Array{Decimal}) map(number, x)
8274

83-
# Check if integer
84-
@deprecate isint(x::Integer) isinteger(x)
85-
@deprecate isint(x::AbstractFloat) isinteger(x)
86-
@deprecate isint(x::AbstractString) isinteger(parse(Float64, x))
75+
# sign
76+
Base.signbit(x::Decimal) = x.s != 0

src/equals.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
# equals() now depends on == instead
44
# of the other way round.
55
function ==(x::Decimal, y::Decimal)
6+
# return early on zero
7+
x_is_zero = iszero(x)
8+
y_is_zero = iszero(y)
9+
if x_is_zero || y_is_zero
10+
return x_is_zero === y_is_zero
11+
end
12+
613
a = normalize(x)
714
b = normalize(y)
815
a.c == b.c && a.q == b.q && a.s == b.s
@@ -16,11 +23,9 @@ function <(x::Decimal, y::Decimal)
1623
return false
1724
end
1825

19-
signdiff = y.s - x.s
20-
if signdiff == 1
21-
return false
22-
elseif signdiff == -1
23-
return true
26+
# avoid normalization if possible
27+
if x.q == y.q
28+
return isless(x.s == 0 ? x.c : -x.c, y.s == 0 ? y.c : -y.c)
2429
end
2530

2631
diff = y - x

src/norm.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ function normalize(x::Decimal; rounded::Bool=false)
1414
round(Decimal(x.s, abs(c), q), digits=DIGITS, normal=true)
1515
end
1616
end
17-
18-
@deprecate norm(x::Decimal; kwargs...) normalize(x; kwargs...)

src/round.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ function round(x::Decimal; digits::Int=0, normal::Bool=false)
99
(normal) ? d : normalize(d, rounded=true)
1010
end
1111
end
12-
13-
@deprecate round(x::Decimal, dpts::Int; normal::Bool=false) round(x, digits=dpts, normal=normal)

test/runtests.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Decimals
2-
using Compat.Test
2+
using Test
33

44
@testset "Decimals" begin
55

@@ -18,6 +18,5 @@ include("test_norm.jl")
1818
include("test_arithmetic.jl")
1919
include("test_equals.jl")
2020
include("test_round.jl")
21-
include("test_deprecated.jl")
2221

2322
end

test/test_arithmetic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Decimals
2-
using Compat.Test
2+
using Test
33

44
@testset "Arithmetic" begin
55

@@ -20,6 +20,7 @@ end
2020
@testset "Negation" begin
2121
@test -Decimal.([0.3 0.2]) == [-Decimal(0.3) -Decimal(0.2)]
2222
@test -Decimal(0.3) == zero(Decimal) - Decimal(0.3)
23+
@test iszero(decimal(12.1) - decimal(12.1))
2324
end
2425

2526
@testset "Multiplication" begin

0 commit comments

Comments
 (0)