Skip to content

Commit 4edb75c

Browse files
committed
exponents > 99
1 parent b502b76 commit 4edb75c

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/fmtcore.jl

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,9 @@ function _pfmt_f(out::IO, fs::FormatSpec, x::AbstractFloat)
202202
end
203203
end
204204

205-
function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::Int, ec::Char)
205+
function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::Integer, ec::Char)
206206
intv = trunc(Integer,u)
207207
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
216208
_pfmt_float(out, sch, zs, intv, decv, prec)
217209
write(out, ec)
218210
if e >= 0
@@ -221,9 +213,10 @@ function _pfmt_floate(out::IO, sch::Char, zs::Integer, u::Real, prec::Int, e::In
221213
write(out, '-')
222214
e = -e
223215
end
224-
(e1, e2) = divrem(e, 10)
225-
write(out, Char('0' + e1))
226-
write(out, Char('0' + e2))
216+
if e < 10
217+
write(out, '0')
218+
end
219+
_pfmt_intdigits(out, e, _Dec())
227220
end
228221

229222

@@ -235,15 +228,21 @@ function _pfmt_e(out::IO, fs::FormatSpec, x::AbstractFloat)
235228
e = 0
236229
u = zero(x)
237230
else
238-
e = floor(Integer,log10(ax)) # exponent
239-
u = ax / exp10(e) # significand
231+
e = floor(Integer, log10(ax))
232+
scale = exp10(-e + fs.prec)
233+
rax = round(ax * scale) / scale
234+
e = floor(Integer, log10(rax)) # exponent
235+
u = rax / exp10(e) # significand
240236
end
241237

242238
# calculate length
243239
xlen = 6 + fs.prec
240+
if abs(e) > 99
241+
xlen += _ndigits(abs(e), _Dec()) - 2
242+
end
244243
if sch != '\0'
245244
xlen += 1
246-
end
245+
end
247246

248247
# print
249248
ec = isupper(fs.typ) ? 'E' : 'e'

test/fmtspec.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ fs = FormatSpec("d")
155155
@test fmt(".1e", 0.6) == "6.0e-01"
156156
@test fmt(".1e", 0.9) == "9.0e-01"
157157

158+
@test fmt("10.2e", 1.2e100) == " 1.20e+100"
159+
@test fmt("11.2e", BigFloat("1.2e1000")) == " 1.20e+1000"
160+
@test fmt("11.2e", BigFloat("1.2e-1000")) == " 1.20e-1000"
161+
@test fmt("9.2e", 9.999e9) == " 1.00e+10"
162+
@test fmt("10.2e", 9.999e99) == " 1.00e+100"
163+
@test fmt("11.2e", BigFloat("9.999e999")) == " 1.00e+1000"
164+
@test fmt("10.2e", -9.999e-100) == " -1.00e-99"
165+
158166
# format special floating point value
159167

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

0 commit comments

Comments
 (0)