Skip to content

Commit

Permalink
Fix overflow for hypot in Float16 (#38322)
Browse files Browse the repository at this point in the history
* Fix overflow for hypot in Float16

Fixes #38311

* hypot tests for ax > floatmax(ax)/2

* hypot Float16 promote to Float32

Co-authored-by: achuchmala <ach@spyro-soft.com>
  • Loading branch information
simonbyrne and achuchmala authored Dec 4, 2020
1 parent 0db72ee commit 8dd959e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ function _hypot(x, y)
end
return h*scale*oneunit(axu)
end
_hypot(x::Float16, y::Float16) = Float16(_hypot(Float32(x), Float32(y)))
_hypot(x::ComplexF16, y::ComplexF16) = Float16(_hypot(ComplexF32(x), ComplexF32(y)))

function _hypot(x...)
maxabs = maximum(abs, x)
if isnan(maxabs) && any(isinf, x)
Expand Down
4 changes: 4 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,10 @@ end
@test hypot(x, x*f) x * hypot(one(f), f) rtol=eps(T)
@test hypot(x, x*f, x*f) x * hypot(one(f), f, f) rtol=eps(T)
end
let x = floatmax(T)/2
@test (@inferred hypot(x, x/4)) x * sqrt(17/BigFloat(16))
@test (@inferred hypot(x, x/4, x/4)) x * sqrt(9/BigFloat(8))
end
end
# hypot on Complex returns Real
@test (@inferred hypot(3, 4im)) === 5.0
Expand Down

0 comments on commit 8dd959e

Please sign in to comment.