From 14171def5d5858d5232e2c329ce3b3171855698b Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Mon, 9 Apr 2018 16:29:02 -0400 Subject: [PATCH 1/4] Update calls to round, change signif to round --- src/fmtcore.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/fmtcore.jl b/src/fmtcore.jl index 488f69b..5f5a802 100644 --- a/src/fmtcore.jl +++ b/src/fmtcore.jl @@ -171,7 +171,7 @@ end function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat) # separate sign, integer, and decimal part - rax = round(abs(x), fs.prec) + rax = round(abs(x); digits=fs.prec) sch = _signchar(x, fs.sign) intv = trunc(Integer, rax) decv = rax - intv @@ -226,7 +226,7 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat) e = 0 u = zero(x) else - rax = signif(ax, fs.prec + 1) + rax = round(ax; sigdigits = fs.prec + 1) e = floor(Integer, log10(rax)) # exponent u = rax * exp10(-e) # significand end From a94b999a1c453cf80714d7336cd76cd9e9536ffa Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Mon, 9 Apr 2018 17:41:53 -0400 Subject: [PATCH 2/4] Update build badges --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4d813d1..12dc72c 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,8 @@ This package offers Python-style general formatting and c-style numerical formatting (for speed). -[![Build Status](https://travis-ci.org/JuliaIO/Formatting.jl.svg?branch=master)](https://travis-ci.org/JuliaIO/Formatting.jl) -[![Formatting](http://pkg.julialang.org/badges/Formatting_0.4.svg)](http://pkg.julialang.org/?pkg=Formatting&ver=0.4) -[![Formatting](http://pkg.julialang.org/badges/Formatting_0.5.svg)](http://pkg.julialang.org/?pkg=Formatting&ver=0.5) +[![Formatting](http://pkg.julialang.org/badges/Formatting_0.6.svg)](http://pkg.julialang.org/?pkg=Formatting&ver=0.6) +[![Formatting](http://pkg.julialang.org/badges/Formatting_0.7.svg)](http://pkg.julialang.org/?pkg=Formatting&ver=0.7) --------------- From 71525f1bde91c69cf5262d9086518e31341cbf3c Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Mon, 9 Apr 2018 21:10:15 -0400 Subject: [PATCH 3/4] Maintain v0.6 compatibility --- src/fmtcore.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/fmtcore.jl b/src/fmtcore.jl index 5f5a802..a9d06d6 100644 --- a/src/fmtcore.jl +++ b/src/fmtcore.jl @@ -171,7 +171,8 @@ end function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat) # separate sign, integer, and decimal part - rax = round(abs(x); digits=fs.prec) + rax = (@static VERSION < v"0.7.0-DEV.4804" ? round(abs(x), fs.prec) : + round(abs(x); digits=fs.prec)) sch = _signchar(x, fs.sign) intv = trunc(Integer, rax) decv = rax - intv @@ -226,7 +227,8 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat) e = 0 u = zero(x) else - rax = round(ax; sigdigits = fs.prec + 1) + rax = (@static VERSION < v"0.7.0-DEV.4804" ? signif(ax, fs.prec + 1) : + round(ax; sigdigits = fs.prec + 1)) e = floor(Integer, log10(rax)) # exponent u = rax * exp10(-e) # significand end From 2cda5828027dddb47d2d78d1d4b7d29587f7ccfc Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Tue, 10 Apr 2018 08:43:10 -0400 Subject: [PATCH 4/4] Workaround broken signif call --- src/fmtcore.jl | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/fmtcore.jl b/src/fmtcore.jl index a9d06d6..4b352a4 100644 --- a/src/fmtcore.jl +++ b/src/fmtcore.jl @@ -219,6 +219,36 @@ function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::In end +# Pull in definition of signif from v0.6.2 base, since it is currently broken for +# BigFloat and DecFP (at least) on master + +@static if VERSION >= v"0.7.0-DEV.4804" +# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212 +# for round, og is the power of 10 relative to the decimal point +# for signif, og is the absolute power of 10 +# digits and base must be integers, x must be convertable to float + +function signif(x::Real, digits::Integer, base::Integer=10) + digits < 1 && throw(DomainError()) + + x = float(x) + (x == 0 || !isfinite(x)) && return x + if base == 10 + e = floor(log10(abs(x)) - digits + 1.) + og = oftype(x, exp10(abs(e))) + elseif base == 2 + e = exponent(abs(x)) - digits + 1. + og = oftype(x, exp2(abs(e))) + else + e = floor(log(base, abs(x)) - digits + 1.) + og = oftype(x, float(base) ^ abs(e)) + end + # for numeric stability + r = e >= 0 ? round(x/og)*og : round(x*og)/og + isfinite(r) ? r : x +end +end + function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat) # extract sign, significand, and exponent ax = abs(x) @@ -227,8 +257,7 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat) e = 0 u = zero(x) else - rax = (@static VERSION < v"0.7.0-DEV.4804" ? signif(ax, fs.prec + 1) : - round(ax; sigdigits = fs.prec + 1)) + rax = signif(ax, fs.prec + 1) # round(ax; sigdigits = fs.prec + 1) e = floor(Integer, log10(rax)) # exponent u = rax * exp10(-e) # significand end