Skip to content

Commit

Permalink
Fix hypot promotion bug (#53541)
Browse files Browse the repository at this point in the history
Fixes #53505
  • Loading branch information
danielwe authored Mar 1, 2024
1 parent 0918cf1 commit ac41e2a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -817,8 +817,8 @@ true
```
"""
hypot(x::Number) = abs(float(x))
hypot(x::Number, y::Number) = _hypot(promote(float(x), y)...)
hypot(x::Number, y::Number, xs::Number...) = _hypot(promote(float(x), y, xs...))
hypot(x::Number, y::Number) = _hypot(float.(promote(x, y))...)
hypot(x::Number, y::Number, xs::Number...) = _hypot(float.(promote(x, y, xs...)))
function _hypot(x, y)
# preserves unit
axu = abs(x)
Expand Down
10 changes: 10 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,16 @@ end
# hypot on Complex returns Real
@test (@inferred hypot(3, 4im)) === 5.0
@test (@inferred hypot(3, 4im, 12)) === 13.0
@testset "promotion, issue #53505" begin
@testset "Int,$T" for T in (Float16, Float32, Float64, BigFloat)
for args in ((3, 4), (3, 4, 12))
for i in eachindex(args)
targs = ntuple(j -> (j == i) ? T(args[j]) : args[j], length(args))
@test (@inferred hypot(targs...)) isa float(eltype(promote(targs...)))
end
end
end
end
end

struct BadFloatWrapper <: AbstractFloat
Expand Down

0 comments on commit ac41e2a

Please sign in to comment.