Skip to content

Commit

Permalink
Fix #34316 (#34817)
Browse files Browse the repository at this point in the history
Fix hypot with both arguments 0

Remove hypot from Furlongs in order to test the defined fallback
  • Loading branch information
SebastianM-C authored and KristofferC committed Apr 11, 2020
1 parent 81623fa commit 60cbbfc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
14 changes: 12 additions & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -629,9 +629,19 @@ julia> hypot(3, 4im)
hypot(x::Number, y::Number) = hypot(promote(x, y)...)
hypot(x::Complex, y::Complex) = hypot(abs(x), abs(y))
hypot(x::T, y::T) where {T<:Real} = hypot(float(x), float(y))
hypot(x::T, y::T) where {T<:Number} = (z = y/x; abs(x) * sqrt(one(z) + z*z))
function hypot(x::T, y::T) where {T<:Number}
if !iszero(x)
z = y/x
z2 = z*z

abs(x) * sqrt(oneunit(z2) + z2)
else
abs(y)
end
end

function hypot(x::T, y::T) where T<:AbstractFloat
#Return Inf if either or both imputs is Inf (Compliance with IEEE754)
# Return Inf if either or both inputs is Inf (Compliance with IEEE754)
if isinf(x) || isinf(y)
return T(Inf)
end
Expand Down
19 changes: 12 additions & 7 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1035,10 +1035,15 @@ end
end
end

isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
using .Main.Furlongs
@test hypot(Furlong(0), Furlong(0)) == Furlong(0.0)
@test hypot(Furlong(3), Furlong(4)) == Furlong(5.0)
@test hypot(Furlong(NaN), Furlong(Inf)) == Furlong(Inf)
@test hypot(Furlong(Inf), Furlong(NaN)) == Furlong(Inf)
@test hypot(Furlong(Inf), Furlong(Inf)) == Furlong(Inf)
@testset "hypot" begin
@test hypot(0, 0) == 0.0
@test hypot(3, 4) == 5.0
@test hypot(NaN, Inf) == Inf
@test hypot(Inf, NaN) == Inf
@test hypot(Inf, Inf) == Inf

isdefined(Main, :Furlongs) || @eval Main include("testhelpers/Furlongs.jl")
using .Main.Furlongs
@test hypot(Furlong(0), Furlong(0)) == Furlong(0.0)
@test hypot(Furlong(3), Furlong(4)) == Furlong(5.0)
end
4 changes: 2 additions & 2 deletions test/testhelpers/Furlongs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ for f in (:real,:imag,:complex,:+,:-)
@eval Base.$f(x::Furlong{p}) where {p} = Furlong{p}($f(x.val))
end

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

0 comments on commit 60cbbfc

Please sign in to comment.