Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overflow for hypot in Float16 #38322

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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