Skip to content

Commit e059f99

Browse files
authored
Updates for Julia 0.7 (#24)
* Updates for Julia 0.7 * Add direct type constructors that call convert
1 parent 5b84e7e commit e059f99

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.6
2-
Compat 0.41
2+
Compat 0.64.0

src/FixedPointDecimals.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ function convert(::Type{TR}, x::FD{T, f}) where {TR <: Rational, T, f}
309309
convert(TR, x.i // coefficient(FD{T, f}))::TR
310310
end
311311

312+
(::Type{T})(x::FD) where {T<:Union{AbstractFloat,Integer,Rational}} = convert(T, x)
313+
312314
promote_rule(::Type{FD{T, f}}, ::Type{<:Integer}) where {T, f} = FD{T, f}
313315
promote_rule(::Type{<:FD}, ::Type{TF}) where {TF <: AbstractFloat} = TF
314316
promote_rule(::Type{<:FD}, ::Type{Rational{TR}}) where {TR} = Rational{TR}
@@ -385,18 +387,18 @@ function parse(::Type{FD{T, f}}, str::AbstractString, mode::RoundingMode=RoundNe
385387
end
386388

387389
# Parse exponent information
388-
exp_index = findfirst(equalto('e'), str)
390+
exp_index = coalesce(findfirst(==('e'), str), 0)
389391
if exp_index > 0
390392
exp = parse(Int, str[(exp_index + 1):end])
391393
sig_end = exp_index - 1
392394
else
393395
exp = 0
394-
sig_end = endof(str)
396+
sig_end = lastindex(str)
395397
end
396398

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

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

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

416418
# Parse the strings
417419
val = isempty(a) ? T(0) : sign * parse(T, a)
418-
if !isempty(b) && any(collect(b[2:end]) .!= '0')
420+
if !isempty(b) && any(!isequal('0'), b[2:end])
419421
if mode == RoundThrows
420422
throw(InexactError(:parse, FD{T, f}, str))
421423
elseif mode == RoundNearest

test/runtests.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
using FixedPointDecimals
22
import FixedPointDecimals: FD, value
33
using Compat
4-
VERSION < v"0.7.0-DEV.2047" && import Compat: Test
5-
import Compat.Printf: @sprintf
6-
using Test
7-
import Base.Checked: checked_mul
4+
using Compat.Test
5+
using Compat.Printf
6+
using Base.Checked: checked_mul
87

98
include("utils.jl")
109

@@ -49,7 +48,7 @@ const keyvalues = Dict(
4948
# Floating point values written as integer strings. Useful for testing behaviours of
5049
# trunc, floor, and ceil.
5150
const INTS = Dict(
52-
v => replace(@sprintf("%.200f", v), ".", "")
51+
v => replace(@sprintf("%.200f", v), "." => "")
5352
for v in [
5453
1.22,
5554
1.23,
@@ -136,6 +135,7 @@ end
136135
@testset for x in keyvalues[FD2]
137136
@testset for T in [Rational{Int128}, WFD2, WFD4]
138137
@test convert(FD2, convert(T, x)) == x
138+
@test T(x) == convert(T, x)
139139
end
140140
if 0 abs(x) < 2
141141
@testset for T in [SFD2, SFD4, FD4]
@@ -774,12 +774,12 @@ end
774774

775775
@testset "show" begin
776776
@testset "compact" begin
777-
@test sprint(showcompact, FD2(1.00)) == "1.0"
778-
@test sprint(showcompact, FD2(1.23)) == "1.23"
779-
@test sprint(showcompact, FD2(42.40)) == "42.4"
780-
@test sprint(showcompact, FD2(-42.40)) == "-42.4"
781-
@test sprint(showcompact, FD2(-0.01)) == "-0.01"
782-
@test sprint(showcompact, FD2(0)) == "0.0"
777+
@test sprintcompact(FD2(1.00)) == "1.0"
778+
@test sprintcompact(FD2(1.23)) == "1.23"
779+
@test sprintcompact(FD2(42.40)) == "42.4"
780+
@test sprintcompact(FD2(-42.40)) == "-42.4"
781+
@test sprintcompact(FD2(-0.01)) == "-0.01"
782+
@test sprintcompact(FD2(0)) == "0.0"
783783

784784
@test repr(typemin(FixedDecimal{Int64, 2})) ==
785785
"FixedDecimal{Int64,2}(-92233720368547758.08)"
@@ -795,7 +795,7 @@ end
795795
@testset "string" begin
796796
for x in keyvalues[FD2]
797797
if 0 abs(x) < 1000
798-
@test eval(Meta.parse(string(x))) == x
798+
@test Core.eval(@__MODULE__, Meta.parse(string(x))) == x
799799
end
800800
end
801801
end

test/utils.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function integer_alt(::Type{T}, dp::Integer, val::AbstractFloat) where {T<:Integ
1919
# perform any rounding.
2020
str = float_string(val)
2121
sign = T(first(str) == '-' ? -1 : 1)
22-
decimal = findfirst(equalto('.'), str)
22+
decimal = coalesce(findfirst(==('.'), str), 0)
2323
int_start = sign < 0 ? 2 : 1
2424
int_end = decimal + dp
2525
v = parse(T, str[int_start:(decimal - 1)] * str[(decimal + 1):int_end])
@@ -41,3 +41,9 @@ function ceil_alt(::Type{FD{T,f}}, val::AbstractFloat) where {T<:Integer, f}
4141
s, v, r = integer_alt(T, f, val)
4242
reinterpret(FD{T,f}, copysign(v + (s > 0 ? r : zero(T)), s))
4343
end
44+
45+
if VERSION < v"0.7.0-DEV.4524"
46+
sprintcompact(x...) = sprint(showcompact, x...)
47+
else
48+
sprintcompact(x...) = sprint(show, x..., context=:compact=>true)
49+
end

0 commit comments

Comments
 (0)