Skip to content

Commit fffd879

Browse files
committed
print Float16 with 5 digits. fixes #5948
not a perfect fix, but better to spend an extra byte than have wrong output
1 parent e8d7e0d commit fffd879

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

base/grisu.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ end
113113

114114
show(io::IO, x::Float64) = _show(io, x, SHORTEST, 0, true)
115115
show(io::IO, x::Float32) = _show(io, x, SHORTEST_SINGLE, 0, true)
116-
show(io::IO, x::Float16) = _show(io, x, PRECISION, 4, true)
116+
show(io::IO, x::Float16) = _show(io, x, PRECISION, 5, true)
117117

118118
print(io::IO, x::Float32) = _show(io, x, SHORTEST_SINGLE, 0, false)
119-
print(io::IO, x::Float16) = _show(io, x, PRECISION, 4, false)
119+
print(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)
120120

121121
showcompact(io::IO, x::Float64) =
122122
(Base._limit_output::Bool) ? _show(io, x, PRECISION, 6, false) : _show(io, x, SHORTEST, 0, false)
123123
showcompact(io::IO, x::Float32) =
124124
(Base._limit_output::Bool) ? _show(io, x, PRECISION, 6, false) : _show(io, x, SHORTEST_SINGLE, 0, false)
125-
showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 4, false)
125+
showcompact(io::IO, x::Float16) = _show(io, x, PRECISION, 5, false)
126126

127127
# normal:
128128
# 0 < pt < len ####.#### len+1
@@ -177,7 +177,7 @@ end
177177

178178
print_shortest(io::IO, x::Float64, dot::Bool) = _print_shortest(io, x, dot, SHORTEST, 0)
179179
print_shortest(io::IO, x::Float32, dot::Bool) = _print_shortest(io, x, dot, SHORTEST_SINGLE, 0)
180-
print_shortest(io::IO, x::Float16, dot::Bool=false) = _print_shortest(io, x, dot, PRECISION, 4)
180+
print_shortest(io::IO, x::Float16, dot::Bool=false) = _print_shortest(io, x, dot, PRECISION, 5)
181181
print_shortest(io::IO, x::Union(FloatingPoint,Integer)) = print_shortest(io, float(x), false)
182182

183183
end # module

test/float16.jl

+3
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,6 @@ let
9292
f = reinterpret(Float32, 0b00111110101010100001000000000000)
9393
@test float32(float16(f)) === reinterpret(Float32, 0b00111110101010100000000000000000)
9494
end
95+
96+
# issue #5948
97+
@test string(reinterpret(Float16, 0x7bff)) == "65504.0"

0 commit comments

Comments
 (0)