Skip to content

Commit ada82ac

Browse files
committed
Fix compact float printing when output contains exactly 6 digits
This was really just a regression when switching from grisu -> ryu algorithm for float printing. The fix is just putting [@vtjnash's code](5d2a0ec#diff-22819a3d47074bbe6530dfef9c94969cR75) for the grisu fix into the ryu branch.
1 parent 7adb9ce commit ada82ac

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

base/ryu/shortest.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ end
330330
olength = decimallength(output)
331331
exp_form = true
332332
pt = nexp + olength
333-
if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision)
333+
if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision) &&
334+
!(pt >= olength && abs(mod(x + 0.05, 10^(pt - olength)) - 0.05) > 0.05)
334335
exp_form = false
335336
if pt <= 0
336337
buf[pos] = UInt8('0')

test/numbers.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,14 +415,16 @@ end
415415
@test repr(-NaN) == "NaN"
416416
@test repr(Float64(pi)) == "3.141592653589793"
417417
# issue 6608
418-
@test sprint(show, 666666.6, context=:compact => true) == "666667.0"
418+
@test sprint(show, 666666.6, context=:compact => true) == "6.66667e5"
419419
@test sprint(show, 666666.049, context=:compact => true) == "666666.0"
420420
@test sprint(show, 666665.951, context=:compact => true) == "666666.0"
421421
@test sprint(show, 66.66666, context=:compact => true) == "66.6667"
422-
@test sprint(show, -666666.6, context=:compact => true) == "-666667.0"
422+
@test sprint(show, -666666.6, context=:compact => true) == "-6.66667e5"
423423
@test sprint(show, -666666.049, context=:compact => true) == "-666666.0"
424424
@test sprint(show, -666665.951, context=:compact => true) == "-666666.0"
425425
@test sprint(show, -66.66666, context=:compact => true) == "-66.6667"
426+
@test sprint(show, -498796.2749933266, context=:compact => true) == "-4.98796e5"
427+
@test sprint(show, 123456.78, context=:compact=>true) == "1.23457e5"
426428

427429
@test repr(1.0f0) == "1.0f0"
428430
@test repr(-1.0f0) == "-1.0f0"

0 commit comments

Comments
 (0)