Skip to content

Commit

Permalink
Fix Float16 conversion from large Float32s (#33084)
Browse files Browse the repository at this point in the history
Fixes #33076
(cherry picked from commit 7bc7904)
  • Loading branch information
Keno authored and JeffBezanson committed Aug 30, 2019
1 parent f69b057 commit 8e092f7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ function Float16(val::Float32)
# NOTE: we maybe should ignore NaNs here, but the payload is
# getting truncated anyway so "rounding" it might not matter
nextbit = (f >> (sh-1)) & 1
if nextbit != 0
if nextbit != 0 && (h & 0x7C00) != 0x7C00
# Round halfway to even or check lower bits
if h&1 == 1 || (f & ((1<<(sh-1))-1)) != 0
h += UInt16(1)
Expand Down
3 changes: 3 additions & 0 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,6 @@ const minsubf16_32 = Float32(minsubf16)
# Ties to even, in this case up
@test Float16(minsubf16_32 + f16eps2) == nextfloat(minsubf16)
@test Float16(prevfloat(minsubf16_32 + f16eps2)) == minsubf16

# issues #33076
@test Float16(1f5) == Inf16

0 comments on commit 8e092f7

Please sign in to comment.