Skip to content

Commit 60cbbfc

Browse files
SebastianM-CKristofferC
authored andcommitted
Fix #34316 (#34817)
Fix hypot with both arguments 0 Remove hypot from Furlongs in order to test the defined fallback
1 parent 81623fa commit 60cbbfc

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

base/math.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,19 @@ julia> hypot(3, 4im)
629629
hypot(x::Number, y::Number) = hypot(promote(x, y)...)
630630
hypot(x::Complex, y::Complex) = hypot(abs(x), abs(y))
631631
hypot(x::T, y::T) where {T<:Real} = hypot(float(x), float(y))
632-
hypot(x::T, y::T) where {T<:Number} = (z = y/x; abs(x) * sqrt(one(z) + z*z))
632+
function hypot(x::T, y::T) where {T<:Number}
633+
if !iszero(x)
634+
z = y/x
635+
z2 = z*z
636+
637+
abs(x) * sqrt(oneunit(z2) + z2)
638+
else
639+
abs(y)
640+
end
641+
end
642+
633643
function hypot(x::T, y::T) where T<:AbstractFloat
634-
#Return Inf if either or both imputs is Inf (Compliance with IEEE754)
644+
# Return Inf if either or both inputs is Inf (Compliance with IEEE754)
635645
if isinf(x) || isinf(y)
636646
return T(Inf)
637647
end

test/math.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,10 +1035,15 @@ end
10351035
end
10361036
end
10371037

1038-
isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
1039-
using .Main.Furlongs
1040-
@test hypot(Furlong(0), Furlong(0)) == Furlong(0.0)
1041-
@test hypot(Furlong(3), Furlong(4)) == Furlong(5.0)
1042-
@test hypot(Furlong(NaN), Furlong(Inf)) == Furlong(Inf)
1043-
@test hypot(Furlong(Inf), Furlong(NaN)) == Furlong(Inf)
1044-
@test hypot(Furlong(Inf), Furlong(Inf)) == Furlong(Inf)
1038+
@testset "hypot" begin
1039+
@test hypot(0, 0) == 0.0
1040+
@test hypot(3, 4) == 5.0
1041+
@test hypot(NaN, Inf) == Inf
1042+
@test hypot(Inf, NaN) == Inf
1043+
@test hypot(Inf, Inf) == Inf
1044+
1045+
isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
1046+
using .Main.Furlongs
1047+
@test hypot(Furlong(0), Furlong(0)) == Furlong(0.0)
1048+
@test hypot(Furlong(3), Furlong(4)) == Furlong(5.0)
1049+
end

test/testhelpers/Furlongs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ for f in (:real,:imag,:complex,:+,:-)
4646
@eval Base.$f(x::Furlong{p}) where {p} = Furlong{p}($f(x.val))
4747
end
4848

49-
import Base: +, -, ==, !=, <, <=, isless, isequal, *, /, //, div, rem, mod, ^, hypot
50-
for op in (:+, :-, :hypot)
49+
import Base: +, -, ==, !=, <, <=, isless, isequal, *, /, //, div, rem, mod, ^
50+
for op in (:+, :-)
5151
@eval function $op(x::Furlong{p}, y::Furlong{p}) where {p}
5252
v = $op(x.val, y.val)
5353
Furlong{p}(v)

0 commit comments

Comments
 (0)