Skip to content

Commit

Permalink
Updates for Julia 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed May 23, 2018
1 parent 5b84e7e commit 14593ad
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
Compat 0.41
Compat 0.64.0
10 changes: 5 additions & 5 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,18 @@ function parse(::Type{FD{T, f}}, str::AbstractString, mode::RoundingMode=RoundNe
end

# Parse exponent information
exp_index = findfirst(equalto('e'), str)
exp_index = coalesce(findfirst(==('e'), str), 0)
if exp_index > 0
exp = parse(Int, str[(exp_index + 1):end])
sig_end = exp_index - 1
else
exp = 0
sig_end = endof(str)
sig_end = lastindex(str)
end

# Remove the decimal place from the string
sign = T(first(str) == '-' ? -1 : 1)
dec_index = findfirst(equalto('.'), str)
dec_index = coalesce(findfirst(==('.'), str), 0)
sig_start = sign < 0 ? 2 : 1
if dec_index > 0
int_str = str[sig_start:(dec_index - 1)] * str[(dec_index + 1):sig_end]
Expand All @@ -407,15 +407,15 @@ function parse(::Type{FD{T, f}}, str::AbstractString, mode::RoundingMode=RoundNe

# Split the integer string into the value we can represent inside the FixedDecimal and
# the remaining digits we'll use during rounding
int_end = endof(int_str)
int_end = lastindex(int_str)
pivot = int_end + exp - (-f)

a = rpad(int_str[1:min(pivot, int_end)], pivot, '0')
b = lpad(int_str[max(pivot, 1):int_end], int_end - pivot + 1, '0')

# Parse the strings
val = isempty(a) ? T(0) : sign * parse(T, a)
if !isempty(b) && any(collect(b[2:end]) .!= '0')
if !isempty(b) && any(!isequal('0'), b[2:end])
if mode == RoundThrows
throw(InexactError(:parse, FD{T, f}, str))
elseif mode == RoundNearest
Expand Down
23 changes: 11 additions & 12 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using FixedPointDecimals
import FixedPointDecimals: FD, value
using Compat
VERSION < v"0.7.0-DEV.2047" && import Compat: Test
import Compat.Printf: @sprintf
using Test
import Base.Checked: checked_mul
using Compat.Test
using Compat.Printf
using Base.Checked: checked_mul

include("utils.jl")

Expand Down Expand Up @@ -49,7 +48,7 @@ const keyvalues = Dict(
# Floating point values written as integer strings. Useful for testing behaviours of
# trunc, floor, and ceil.
const INTS = Dict(
v => replace(@sprintf("%.200f", v), ".", "")
v => replace(@sprintf("%.200f", v), "." => "")
for v in [
1.22,
1.23,
Expand Down Expand Up @@ -774,12 +773,12 @@ end

@testset "show" begin
@testset "compact" begin
@test sprint(showcompact, FD2(1.00)) == "1.0"
@test sprint(showcompact, FD2(1.23)) == "1.23"
@test sprint(showcompact, FD2(42.40)) == "42.4"
@test sprint(showcompact, FD2(-42.40)) == "-42.4"
@test sprint(showcompact, FD2(-0.01)) == "-0.01"
@test sprint(showcompact, FD2(0)) == "0.0"
@test sprintcompact(FD2(1.00)) == "1.0"
@test sprintcompact(FD2(1.23)) == "1.23"
@test sprintcompact(FD2(42.40)) == "42.4"
@test sprintcompact(FD2(-42.40)) == "-42.4"
@test sprintcompact(FD2(-0.01)) == "-0.01"
@test sprintcompact(FD2(0)) == "0.0"

@test repr(typemin(FixedDecimal{Int64, 2})) ==
"FixedDecimal{Int64,2}(-92233720368547758.08)"
Expand All @@ -795,7 +794,7 @@ end
@testset "string" begin
for x in keyvalues[FD2]
if 0 abs(x) < 1000
@test eval(Meta.parse(string(x))) == x
@test Core.eval(@__MODULE__, Meta.parse(string(x))) == x
end
end
end
Expand Down
8 changes: 7 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function integer_alt(::Type{T}, dp::Integer, val::AbstractFloat) where {T<:Integ
# perform any rounding.
str = float_string(val)
sign = T(first(str) == '-' ? -1 : 1)
decimal = findfirst(equalto('.'), str)
decimal = coalesce(findfirst(==('.'), str), 0)
int_start = sign < 0 ? 2 : 1
int_end = decimal + dp
v = parse(T, str[int_start:(decimal - 1)] * str[(decimal + 1):int_end])
Expand All @@ -41,3 +41,9 @@ function ceil_alt(::Type{FD{T,f}}, val::AbstractFloat) where {T<:Integer, f}
s, v, r = integer_alt(T, f, val)
reinterpret(FD{T,f}, copysign(v + (s > 0 ? r : zero(T)), s))
end

if VERSION < v"0.7.0-DEV.4524"
sprintcompact(x...) = sprint(showcompact, x...)
else
sprintcompact(x...) = sprint(show, x..., context=:compact=>true)
end

0 comments on commit 14593ad

Please sign in to comment.