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

improved implementation of hypot(a,b) #31922

Merged
merged 12 commits into from
Jun 13, 2019
47 changes: 47 additions & 0 deletions base/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ sqrt(x::Real) = sqrt(float(x))

Compute the hypotenuse ``\\sqrt{x^2+y^2}`` avoiding overflow and underflow.

This code is an implementation of the algorithm described in:
An Improved Algorithm for `hypot(a,b)`
by Carlos F. Borges
The article is available online at ArXiv at the link
https://arxiv.org/abs/1904.09481

# Examples
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> a = 10^10;
Expand All @@ -538,6 +544,47 @@ Stacktrace:
```
"""
hypot(x::Number, y::Number) = hypot(promote(x, y)...)
hypot(x::Complex, y::Complex) = hypot(promote(abs(x),abs(y))...)
hypot(x::Integer, y::Integer) = hypot(promote(float(x), float(y))...)
function hypot(x::T,y::T) where T<:AbstractFloat
#Return Inf if either or both imputs is Inf (Compliance with IEEE754)
if isinf(x) || isinf(y)
return convert(T,Inf)
end

# Order the operands
ax,ay = abs(x), abs(y)
if ay > ax
ax,ay = ay,ax
end

# Widely varying operands
if ay <= ax*sqrt(eps(T)/2) #Note: This also gets ay == 0
return ax
end

# Operands do not vary widely
scale = eps(sqrt(floatmin(T))) #Rescaling constant
if ax > sqrt(floatmax(T)/2)
ax = ax*scale
ay = ay*scale
scale = inv(scale)
elseif ay < sqrt(floatmin(T))
ax = ax/scale
ay = ay/scale
else
scale = one(scale)
end
h = sqrt(muladd(ax,ax,ay*ay))
if h <= 2*ay
delta = h-ay
h -= muladd(delta,delta-2*(ax-ay),ax*(2*delta - ax))/(2*h)
else
delta = h-ax
h -= muladd(delta,delta,muladd(ay,(4*delta-ay),2*delta*(ax-2*ay)))/(2*h)
end
h*scale
end
function hypot(x::T, y::T) where T<:Number
ax = abs(x)
ay = abs(y)
Expand Down
3 changes: 3 additions & 0 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ end
@test isequal(expm1(T(0)), T(0))
@test expm1(T(1)) ≈ T(ℯ)-1 atol=10*eps(T)
@test isequal(hypot(T(3),T(4)), T(5))
@test isequal(hypot(floatmax(T),T(1)),floatmax(T))
@test isequal(hypot(floatmin(T)*sqrt(eps(T)),T(0)),floatmin(T)*sqrt(eps(T)))
@test isequal(floatmin(T)*hypot(1.368423059742933,1.3510496552495361),hypot(floatmin(T)*1.368423059742933,floatmin(T)*1.3510496552495361))
@test isequal(log(T(1)), T(0))
@test isequal(log(ℯ,T(1)), T(0))
@test log(T(ℯ)) ≈ T(1) atol=eps(T)
Expand Down