Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #56 from JuliaString/spj/round
Browse files Browse the repository at this point in the history
Update calls to round, change signif to round
  • Loading branch information
jmkuhn authored Apr 10, 2018
2 parents 016d84a + 2cda582 commit 531efe6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

---------------

Expand Down
35 changes: 33 additions & 2 deletions src/fmtcore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ end

function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
# separate sign, integer, and decimal part
rax = round(abs(x), 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
Expand Down Expand Up @@ -218,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)
Expand All @@ -226,7 +257,7 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
e = 0
u = zero(x)
else
rax = signif(ax, 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 531efe6

Please sign in to comment.