Skip to content

Commit

Permalink
Merge pull request #30 from invenia/ed/upgrade
Browse files Browse the repository at this point in the history
Upgrade and fix
  • Loading branch information
ararslan authored Feb 21, 2019
2 parents 18f72ce + e46326f commit 9b2bcec
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 80 deletions.
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ language: julia
notifications:
email: false
julia:
- 0.6
- 0.7
- 1.0
- 1.1
- nightly
script:
after_success:
- |
julia -e '
VERSION >= v"0.7.0-DEV.3656" && using Pkg
VERSION >= v"0.7.0-DEV.5183" || cd(Pkg.dir("Decimals"))
using Pkg
Pkg.add("Coverage")
using Coverage
Coveralls.submit(Coveralls.process_folder())
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.6
Compat 0.33
julia 0.7
12 changes: 3 additions & 9 deletions src/Decimals.jl
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
# Pure Julia decimal arithmetic
# @license MIT
# @author [email protected] (Jack Peterson), 7/3/2014
VERSION < v"0.7.0-beta2.199" && __precompile__()

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

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

export Decimal,
decimal,
number
number,
normalize

const DIGITS = 20

# Numerical value: (-1)^s * c * 10^q
struct Decimal <: Real
struct Decimal <: AbstractFloat
s::Integer # sign can be 0 (+) or 1 (-)
c::BigInt # coefficient (significand), must be non-negative
q::Integer # exponent
Expand Down
9 changes: 0 additions & 9 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@ function +(x::Decimal, y::Decimal)
c = BigInt(cx) + BigInt(cy)
normalize(Decimal(s, abs(c), min(x.q, y.q)))
end
@deprecate (+)(x::Number, y::Array{Decimal}) x .+ y
@deprecate (+)(x::Array{Decimal}, y::Number) x .+ y
@deprecate (+)(x::Array{<:Number}, y::Array{Decimal}) x .+ y
@deprecate (+)(x::Array{Decimal}, y::Array{<:Number}) x .+ y

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

# Subtraction
-(x::Decimal, y::Decimal) = +(x, -y)
@deprecate (-)(x::Number, y::Array{Decimal}) x .- y
@deprecate (-)(x::Array{Decimal}, y::Number) x .- y
@deprecate (-)(x::Array{<:Number}, y::Array{Decimal}) x .- y
@deprecate (-)(x::Array{Decimal}, y::Array{<:Number}) x .- y

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

# Division
/(x::Decimal, y::Decimal) = x * inv(y)
Expand Down
14 changes: 2 additions & 12 deletions src/decimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ end

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

# convert an array to an array of decimals
@deprecate decimal(x::Array) map(decimal, x)

# Convert a number to a decimal
Decimal(num::Real) = parse(Decimal, string(num))
Base.convert(::Type{Decimal}, num::Real) = Decimal(num::Real)
Expand Down Expand Up @@ -66,10 +63,6 @@ end
Base.zero(::Type{Decimal}) = Decimal(0,0,0)
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(Float64, x)

# convert a decimal to any subtype of Real
(::Type{T})(x::Decimal) where {T<:Real} = parse(T, string(x))

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

# Check if integer
@deprecate isint(x::Integer) isinteger(x)
@deprecate isint(x::AbstractFloat) isinteger(x)
@deprecate isint(x::AbstractString) isinteger(parse(Float64, x))
# sign
Base.signbit(x::Decimal) = x.s != 0
15 changes: 10 additions & 5 deletions src/equals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
# equals() now depends on == instead
# of the other way round.
function ==(x::Decimal, y::Decimal)
# return early on zero
x_is_zero = iszero(x)
y_is_zero = iszero(y)
if x_is_zero || y_is_zero
return x_is_zero === y_is_zero
end

a = normalize(x)
b = normalize(y)
a.c == b.c && a.q == b.q && a.s == b.s
Expand All @@ -16,11 +23,9 @@ function <(x::Decimal, y::Decimal)
return false
end

signdiff = y.s - x.s
if signdiff == 1
return false
elseif signdiff == -1
return true
# avoid normalization if possible
if x.q == y.q
return isless(x.s == 0 ? x.c : -x.c, y.s == 0 ? y.c : -y.c)
end

diff = y - x
Expand Down
2 changes: 0 additions & 2 deletions src/norm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@ function normalize(x::Decimal; rounded::Bool=false)
round(Decimal(x.s, abs(c), q), digits=DIGITS, normal=true)
end
end

@deprecate norm(x::Decimal; kwargs...) normalize(x; kwargs...)
2 changes: 0 additions & 2 deletions src/round.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ function round(x::Decimal; digits::Int=0, normal::Bool=false)
(normal) ? d : normalize(d, rounded=true)
end
end

@deprecate round(x::Decimal, dpts::Int; normal::Bool=false) round(x, digits=dpts, normal=normal)
3 changes: 1 addition & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Decimals" begin

Expand All @@ -18,6 +18,5 @@ include("test_norm.jl")
include("test_arithmetic.jl")
include("test_equals.jl")
include("test_round.jl")
include("test_deprecated.jl")

end
3 changes: 2 additions & 1 deletion test/test_arithmetic.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Arithmetic" begin

Expand All @@ -20,6 +20,7 @@ end
@testset "Negation" begin
@test -Decimal.([0.3 0.2]) == [-Decimal(0.3) -Decimal(0.2)]
@test -Decimal(0.3) == zero(Decimal) - Decimal(0.3)
@test iszero(decimal(12.1) - decimal(12.1))
end

@testset "Multiplication" begin
Expand Down
2 changes: 1 addition & 1 deletion test/test_constructor.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Decimal constructor" begin

Expand Down
2 changes: 1 addition & 1 deletion test/test_decimal.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Conversions" begin

Expand Down
25 changes: 0 additions & 25 deletions test/test_deprecated.jl

This file was deleted.

14 changes: 11 additions & 3 deletions test/test_equals.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using Decimals
using Compat.Test
using Test

@testset "Equality" begin

@testset "isequal" begin
@test isequal(Decimal(0, 2, -3), Decimal(0, 2, -3))
@test ~isequal(Decimal(0, 2, -3), Decimal(0, 2, 3))
@test !isequal(Decimal(0, 2, -3), Decimal(0, 2, 3))
@test isequal(Decimal(0, 2, -3), 0.002)
@test isequal(Decimal(1, 2, 0), -2)
@test ~isequal(Decimal(1, 2, 0), 2)
@test !isequal(Decimal(1, 2, 0), 2)
@test !isequal(Decimal(1, 0, -1), Decimal(0, 0, 0))
end

@testset "==" begin
Expand All @@ -29,6 +30,10 @@ end
bi = big"4608230166434464229556241992703"
@test Decimal(bi) == bi
@test bi == Decimal(bi)

@test decimal(12.1) == decimal(12.1)

@test Decimal(1, 0, -1) == Decimal(0, 0, 0)
end

@testset "<" begin
Expand All @@ -38,6 +43,9 @@ end
@test !(Decimal(1, 0, 1) < Decimal(1, 1, 1))
@test Decimal(0, 2, -3) < Decimal(0, 2, 3)
@test !(Decimal(0, 2, 3) < Decimal(0, 2, -3))
@test !(decimal(12.1) < decimal(12.1))
@test !(Decimal(1, 0, -1) < Decimal(0, 0, 0))
@test !(Decimal(0, 0, 0) < Decimal(1, 0, -1))
end

end
2 changes: 1 addition & 1 deletion test/test_norm.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Normalization" begin

Expand Down
2 changes: 1 addition & 1 deletion test/test_round.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Decimals
using Compat.Test
using Test

@testset "Rounding" begin

Expand Down

0 comments on commit 9b2bcec

Please sign in to comment.