Skip to content

Commit eb903be

Browse files
committed
Merge pull request #6 from odow/fixrounding
Fix round off error
2 parents d8b4bd1 + ce86c2c commit eb903be

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

REQUIRE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
julia 0.2-
1+
julia 0.3-
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

src/fmtspec.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,10 @@ function FormatSpec(s::String)
121121
if a4[1] == '0'
122122
_zpad = true
123123
if length(a4) > 1
124-
_width = parseint(Int,a4[2:end])
124+
_width = parse(Int,a4[2:end])
125125
end
126126
else
127-
_width = parseint(Int,a4)
127+
_width = parse(Int,a4)
128128
end
129129
end
130130

@@ -135,7 +135,7 @@ function FormatSpec(s::String)
135135

136136
# a6 [.prec]
137137
if a6 != nothing
138-
_prec = parseint(Int,a6[2:end])
138+
_prec = parse(Int,a6[2:end])
139139
end
140140

141141
# a7: [type]

src/formatexpr.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ function make_argspec(s::String, pos::Int)
2828
if !isempty(s)
2929
ifil = searchindex(s, "|>")
3030
if ifil == 0
31-
iarg = parseint(Int,s)
31+
iarg = parse(Int,s)
3232
else
33-
iarg = ifil > 1 ? parseint(Int,s[1:ifil-1]) : -1
33+
iarg = ifil > 1 ? parse(Int,s[1:ifil-1]) : -1
3434
hasfil = true
3535
ff = eval(symbol(s[ifil+2:end]))
3636
end

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)