Skip to content

Commit 19848c8

Browse files
authored
Merge pull request #32 from JuliaString/spj/signif
Work around broken signif call on master
2 parents 2bca7d2 + 81f8c67 commit 19848c8

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/fmtcore.jl

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ function _pfmt_floate(out::IO, sch::AbstractChar, zs::Integer, u::Real, prec::In
203203
_pfmt_intdigits(out, e, _Dec())
204204
end
205205

206+
# Pull in definition of signif from v0.6.2 base, since it is currently broken for
207+
# BigFloat and DecFP (at least) on master
208+
209+
@static if VERSION >= v"0.7.0-DEV.4804"
210+
# adapted from Matlab File Exchange roundsd: http://www.mathworks.com/matlabcentral/fileexchange/26212
211+
# for round, og is the power of 10 relative to the decimal point
212+
# for signif, og is the absolute power of 10
213+
# digits and base must be integers, x must be convertable to float
214+
215+
function signif(x::Real, digits::Integer, base::Integer=10)
216+
digits < 1 && throw(DomainError())
217+
218+
x = float(x)
219+
(x == 0 || !isfinite(x)) && return x
220+
if base == 10
221+
e = floor(log10(abs(x)) - digits + 1.)
222+
og = oftype(x, exp10(abs(e)))
223+
elseif base == 2
224+
e = exponent(abs(x)) - digits + 1.
225+
og = oftype(x, exp2(abs(e)))
226+
else
227+
e = floor(log(base, abs(x)) - digits + 1.)
228+
og = oftype(x, float(base) ^ abs(e))
229+
end
230+
# for numeric stability
231+
r = e >= 0 ? round(x/og)*og : round(x*og)/og
232+
isfinite(r) ? r : x
233+
end
234+
end
206235

207236
function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
208237
# extract sign, significand, and exponent
@@ -212,8 +241,7 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
212241
e = 0
213242
u = zero(x)
214243
else
215-
rax = (@static VERSION < v"0.7.0-DEV.4804" ? signif(ax, fs.prec + 1) :
216-
round(ax; sigdigits = fs.prec + 1))
244+
rax = signif(ax, fs.prec + 1) # round(ax; sigdigits = fs.prec + 1)
217245
e = floor(Integer, log10(rax)) # exponent
218246
u = rax * exp10(-e) # significand
219247
end

0 commit comments

Comments
 (0)