Skip to content

Commit

Permalink
Merge pull request #32 from JuliaString/spj/signif
Browse files Browse the repository at this point in the history
Work around broken signif call on master
  • Loading branch information
ScottPJones authored Apr 10, 2018
2 parents 2bca7d2 + 81f8c67 commit 19848c8
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/fmtcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,35 @@ function _pfmt_floate(out::IO, sch::AbstractChar, zs::Integer, u::Real, prec::In
_pfmt_intdigits(out, e, _Dec())
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
Expand All @@ -212,8 +241,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
Expand Down

0 comments on commit 19848c8

Please sign in to comment.