Skip to content

Commit b946add

Browse files
committed
fix rounding error. add tests to cover
1 parent a48d166 commit b946add

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

REQUIRE

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

src/fmtcore.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ function _pfmt_float(out::IO, sch::Char, zs::Integer, intv::Real, decv::Real, pr
148148
if zs > 0
149149
_repwrite(out, '0', zs)
150150
end
151+
idecv = round(Integer, decv * exp10(prec))
152+
if idecv == exp10(prec)
153+
intv += 1
154+
idecv = 0
155+
end
151156
# print integer part
152157
if intv == 0
153158
write(out, '0')
@@ -158,7 +163,6 @@ function _pfmt_float(out::IO, sch::Char, zs::Integer, intv::Real, decv::Real, pr
158163
write(out, '.')
159164
# print decimal part
160165
if prec > 0
161-
idecv = round(Integer, decv * exp10(prec))
162166
nd = _ndigits(idecv, _Dec())
163167
if nd < prec
164168
_repwrite(out, '0', prec - nd)
@@ -201,6 +205,14 @@ end
201205
function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::Int, ec::Char)
202206
intv = trunc(Integer,u)
203207
decv = u - intv
208+
if round(Integer, decv * exp10(prec)) == exp10(prec)
209+
intv += 1
210+
if intv == 10
211+
intv = 1
212+
e += 1
213+
end
214+
decv = 0.
215+
end
204216
_pfmt_float(out, sch, zs, intv, decv, prec)
205217
write(out, ec)
206218
if e >= 0

test/fmtspec.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ fs = FormatSpec("d")
121121
@test fmt("*<8.2f", -8.376) == "-8.38***"
122122
@test fmt("*>8.2f", -8.376) == "***-8.38"
123123

124+
@test fmt(".2f", 0.999) == "1.00"
125+
@test fmt(".2f", 0.996) == "1.00"
126+
# Floating point error can upset this one (i.e. 0.99500000 or 0.994999999)
127+
@test (fmt(".2f", 0.995) == "1.00" || fmt(".2f", 0.995) == "0.99")
128+
@test fmt(".2f", 0.994) == "0.99"
129+
124130
# format floating point (e)
125131

126132
@test fmt("E", 0.0) == "0.000000E+00"
@@ -141,6 +147,14 @@ fs = FormatSpec("d")
141147
@test fmt("012.2e", -13.89) == "-0001.39e+01"
142148
@test fmt("+012.2e", 13.89) == "+0001.39e+01"
143149

150+
@test fmt(".1e", 0.999) == "1.0e+00"
151+
@test fmt(".1e", 0.996) == "1.0e+00"
152+
# Floating point error can upset this one (i.e. 0.99500000 or 0.994999999)
153+
@test (fmt(".1e", 0.995) == "1.0e+00" || fmt(".1e", 0.995) == "9.9e-01")
154+
@test fmt(".1e", 0.994) == "9.9e-01"
155+
@test fmt(".1e", 0.6) == "6.0e-01"
156+
@test fmt(".1e", 0.9) == "9.0e-01"
157+
144158
# format special floating point value
145159

146160
@test fmt("f", NaN) == "NaN"

0 commit comments

Comments
 (0)