From ac41e2a0d446de8249733d902bb774695c9d61f7 Mon Sep 17 00:00:00 2001 From: Daniel Wennberg Date: Fri, 1 Mar 2024 16:24:30 +0100 Subject: [PATCH] Fix `hypot` promotion bug (#53541) Fixes #53505 --- base/math.jl | 4 ++-- test/math.jl | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/base/math.jl b/base/math.jl index 71a4ef9f34882..c3c15505c5f8f 100644 --- a/base/math.jl +++ b/base/math.jl @@ -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) diff --git a/test/math.jl b/test/math.jl index f3c6dc5bb103f..f551bb3a5d4b7 100644 --- a/test/math.jl +++ b/test/math.jl @@ -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