From 5d2a0ec06761ea2b445909757d7744b9f7a93072 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash@gmail.com>
Date: Sat, 18 Apr 2015 11:42:56 -0400
Subject: [PATCH] use exact math to check for rounding in printout to really
 fix #9072 (and fix #6608 too)

---
 base/grisu.jl   | 4 +++-
 test/float16.jl | 4 +++-
 test/numbers.jl | 9 +++++++++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/base/grisu.jl b/base/grisu.jl
index 870ab8d115d8d..ff64821a01643 100644
--- a/base/grisu.jl
+++ b/base/grisu.jl
@@ -71,7 +71,9 @@ function _show(io::IO, x::FloatingPoint, mode, n::Int, typed, nanstr, infstr)
         end
     end
     neg && write(io,'-')
-    if pt <= -4 || pt > 6 || (pt >= n && mode != SHORTEST) # .00001 to 100000.
+    exp_form = pt <= -4 || pt > 6
+    exp_form = exp_form || (pt >= len && abs(mod(x + 0.05, 10^(pt - len)) - 0.05) > 0.05) # see issue #6608
+    if exp_form # .00001 to 100000.
         # => #.#######e###
         write(io, pdigits, 1)
         write(io, '.')
diff --git a/test/float16.jl b/test/float16.jl
index ffc3ecd8a7d8d..a0b5af67d2819 100644
--- a/test/float16.jl
+++ b/test/float16.jl
@@ -55,6 +55,8 @@ g = Float16(1.)
 @test repr(Inf16) == "Inf16"
 @test sprint(showcompact, Inf16) == "Inf"
 
+@test repr(Float16(44099)) == "Float16(4.41e4)"
+
 for z1 in (Float16(0.0), Float16(-0.0)), z2 in (Float16(0.0), Float16(-0.0))
     @test z1 == z2
     @test isequal(z1, z1)
@@ -94,7 +96,7 @@ let
 end
 
 # issue #5948
-@test string(reinterpret(Float16, 0x7bff)) == "65500.0"
+@test string(reinterpret(Float16, 0x7bff)) == "6.55e4"
 
 @test log10(Float16(100)) == Float16(2.0)
 
diff --git a/test/numbers.jl b/test/numbers.jl
index 3e83c2e994e7c..963056f8c002d 100644
--- a/test/numbers.jl
+++ b/test/numbers.jl
@@ -402,6 +402,15 @@ end
 @test repr(NaN) == "NaN"
 @test repr(-NaN) == "NaN"
 @test repr(Float64(pi)) == "3.141592653589793"
+# issue 6608
+@test sprint(showcompact, 666666.6) == "6.66667e5"
+@test sprint(showcompact, 666666.049) == "666666.0"
+@test sprint(showcompact, 666665.951) == "666666.0"
+@test sprint(showcompact, 66.66666) == "66.6667"
+@test sprint(showcompact, -666666.6) == "-6.66667e5"
+@test sprint(showcompact, -666666.049) == "-666666.0"
+@test sprint(showcompact, -666665.951) == "-666666.0"
+@test sprint(showcompact, -66.66666) == "-66.6667"
 
 @test repr(1.0f0) == "1.0f0"
 @test repr(-1.0f0) == "-1.0f0"