hypot

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

Base.Math.hypotFunction
hypot(x, y)

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

hypot(x...)

Compute the hypotenuse $\sqrt{\sum |x_i|^2}$ avoiding overflow and underflow.

See also norm in the LinearAlgebra standard library.

Examples

julia> a = Int64(10)^10;

julia> hypot(a, a)
1.4142135623730951e10

julia> √(a^2 + a^2) # a^2 overflows
ERROR: DomainError with -2.914184810805068e18:
sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
[...]

julia> hypot(3, 4im)
5.0

julia> hypot(-5.7)
5.7

julia> hypot(3, 4im, 12.0)
13.0

julia> using LinearAlgebra

julia> norm([a, a, a, a]) == hypot(a, a, a, a)
true
source

Methods

julia> methods(hypot, (Any,), [Base, Base.Math, Base.MathConstants, Base.MPFR])# 1 method for generic function "hypot" from Base.Math:
 [1] hypot(x::Number)
     @ math.jl:705

Examples

Real Numbers

julia> hypot(3, 4)
5.0

julia> hypot(5, 12)
13.0

julia> hypot(65, 72)
97.0

Tips

See Also

Extended Inputs

Tech Notes

  • hypot(::Real): by pure julia
  • hypot(::BigFloat): by MPFR

Version History

Introduced in Julia v1.0 (2018)