Skip to content

Commit

Permalink
use exact math to check for rounding in printout to really fix #9072
Browse files Browse the repository at this point in the history
…(and fix #6608 too)
  • Loading branch information
vtjnash committed Apr 19, 2015
1 parent 56fe9e5 commit 5d2a0ec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, '.')
Expand Down
4 changes: 3 additions & 1 deletion test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5d2a0ec

Please sign in to comment.