From ada82ac25eb821cdda24dcc38aeea4cfb2c97703 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Mon, 27 Jul 2020 07:26:04 -0600 Subject: [PATCH] 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](https://github.com/JuliaLang/julia/commit/5d2a0ec06761ea2b445909757d7744b9f7a93072#diff-22819a3d47074bbe6530dfef9c94969cR75) for the grisu fix into the ryu branch. --- base/ryu/shortest.jl | 3 ++- test/numbers.jl | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/base/ryu/shortest.jl b/base/ryu/shortest.jl index 641f0116729f4..e1463cad63a45 100644 --- a/base/ryu/shortest.jl +++ b/base/ryu/shortest.jl @@ -330,7 +330,8 @@ end olength = decimallength(output) exp_form = true pt = nexp + olength - if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision) + if -4 < pt <= (precision == -1 ? (T == Float16 ? 3 : 6) : precision) && + !(pt >= olength && abs(mod(x + 0.05, 10^(pt - olength)) - 0.05) > 0.05) exp_form = false if pt <= 0 buf[pos] = UInt8('0') diff --git a/test/numbers.jl b/test/numbers.jl index 5a6d0197397f5..922f313254cb2 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -415,14 +415,16 @@ end @test repr(-NaN) == "NaN" @test repr(Float64(pi)) == "3.141592653589793" # issue 6608 - @test sprint(show, 666666.6, context=:compact => true) == "666667.0" + @test sprint(show, 666666.6, context=:compact => true) == "6.66667e5" @test sprint(show, 666666.049, context=:compact => true) == "666666.0" @test sprint(show, 666665.951, context=:compact => true) == "666666.0" @test sprint(show, 66.66666, context=:compact => true) == "66.6667" - @test sprint(show, -666666.6, context=:compact => true) == "-666667.0" + @test sprint(show, -666666.6, context=:compact => true) == "-6.66667e5" @test sprint(show, -666666.049, context=:compact => true) == "-666666.0" @test sprint(show, -666665.951, context=:compact => true) == "-666666.0" @test sprint(show, -66.66666, context=:compact => true) == "-66.6667" + @test sprint(show, -498796.2749933266, context=:compact => true) == "-4.98796e5" + @test sprint(show, 123456.78, context=:compact=>true) == "1.23457e5" @test repr(1.0f0) == "1.0f0" @test repr(-1.0f0) == "-1.0f0"