Skip to content

Commit

Permalink
print Float16 with 5 digits. fixes JuliaLang#5948
Browse files Browse the repository at this point in the history
not a perfect fix, but better to spend an extra byte than have wrong output
  • Loading branch information
JeffBezanson committed Feb 25, 2014
1 parent e8d7e0d commit fffd879
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ end

show(io::IO, x::Float64) = _show(io, x, SHORTEST, 0, true)
show(io::IO, x::Float32) = _show(io, x, SHORTEST_SINGLE, 0, true)
show(io::IO, x::Float16) = _show(io, x, PRECISION, 4, true)
show(io::IO, x::Float16) = _show(io, x, PRECISION, 5, true)

print(io::IO, x::Float32) = _show(io, x, SHORTEST_SINGLE, 0, false)
print(io::IO, x::Float16) = _show(io, x, PRECISION, 4, false)
print(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)

showcompact(io::IO, x::Float64) =
(Base._limit_output::Bool) ? _show(io, x, PRECISION, 6, false) : _show(io, x, SHORTEST, 0, false)
showcompact(io::IO, x::Float32) =
(Base._limit_output::Bool) ? _show(io, x, PRECISION, 6, false) : _show(io, x, SHORTEST_SINGLE, 0, false)
showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 4, false)
showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)

# normal:
# 0 < pt < len ####.#### len+1
Expand Down Expand Up @@ -177,7 +177,7 @@ end

print_shortest(io::IO, x::Float64, dot::Bool) = _print_shortest(io, x, dot, SHORTEST, 0)
print_shortest(io::IO, x::Float32, dot::Bool) = _print_shortest(io, x, dot, SHORTEST_SINGLE, 0)
print_shortest(io::IO, x::Float16, dot::Bool=false) = _print_shortest(io, x, dot, PRECISION, 4)
print_shortest(io::IO, x::Float16, dot::Bool=false) = _print_shortest(io, x, dot, PRECISION, 5)
print_shortest(io::IO, x::Union(FloatingPoint,Integer)) = print_shortest(io, float(x), false)

end # module
3 changes: 3 additions & 0 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,6 @@ let
f = reinterpret(Float32, 0b00111110101010100001000000000000)
@test float32(float16(f)) === reinterpret(Float32, 0b00111110101010100000000000000000)
end

# issue #5948
@test string(reinterpret(Float16, 0x7bff)) == "65504.0"

0 comments on commit fffd879

Please sign in to comment.