Math.hypot
func Math.hypot(x: float64, y: float64): float64Math.hypot returns the square root of x² + y², without overflow in the intermediate steps.
Details
- The result is never negative: the signs of
xandydo not matter. Math.hypotnever panics. A result too large for afloat64isInfinity.
Basic examples
Math.hypot(3.0, 4.0) // 5.0
Math.hypot(5.0, 12.0) // 13.0
Math.hypot(1.0, 1.0) // 1.4142135623730951Scope
Negative arguments give the same result as positive ones:
Math.hypot(-3.0, 4.0) // 5.0
Math.hypot(-3.0, -4.0) // 5.0With one argument zero, the result is the absolute value of the other:
Math.hypot(7.0, 0.0) // 7.0
Math.hypot(-7.0, 0.0) // 7.0
Math.hypot(0.0, 0.0) // 0.0Very large and very small arguments, where squaring alone would overflow or underflow:
Math.hypot(1e200, 1e200) // 1.414213562373095e+200
Math.hypot(1e-200, 1e-200) // 1.414213562373095e-200Infinities and NaN:
Math.hypot(Math.Infinity, 2.0) // Infinity
Math.hypot(-Math.Infinity, 2.0) // Infinity
Math.hypot(Math.NaN, 2.0) // NaN
Math.hypot(Math.Infinity, Math.NaN) // InfinityA result past the largest float64 is Infinity:
Math.hypot(Math.maxFloat64, Math.maxFloat64) // InfinityName the arguments:
Math.hypot(x: 6.0, y: 8.0) // 10.0In a pipe, the value is x:
3.0 | Math.hypot(4.0) // 5.0Applications
The distance between two points:
type Point = { x: float64, y: float64 }
func distance(a: Point, b: Point): float64 {
Math.hypot(b.x - a.x, b.y - a.y)
}
distance(Point{ x: 1.0, y: 1.0 }, Point{ x: 4.0, y: 5.0 }) // 5.0The length of a path through several points:
type Point = { x: float64, y: float64 }
let path = [Point{ x: 0.0, y: 0.0 }, Point{ x: 3.0, y: 4.0 }, Point{ x: 3.0, y: 10.0 }]
var total = 0.0
for i in Iter.range(1, path.length()) {
total += Math.hypot(path[i].x - path[i - 1].x, path[i].y - path[i - 1].y)
}
total // 11.0The length of a vector in three dimensions, by nesting calls:
Math.hypot(Math.hypot(1.0, 2.0), 2.0) // 3.0The point nearest to the origin:
type Point = { x: float64, y: float64 }
let points = [Point{ x: 3.0, y: 4.0 }, Point{ x: -1.0, y: 2.0 }, Point{ x: 0.5, y: -2.5 }]
points.minBy({ Math.hypot(x.x, x.y) }) // Point{ x: -1.0, y: 2.0 }Polar coordinates, with Math.atan2 for the angle:
let x = 1.0
let y = 1.0
Math.hypot(x, y) // 1.4142135623730951
Math.atan2(y, x) // 0.7853981633974483Properties and relations
Math.hypot is symmetric and ignores signs:
for [x, y] in [[3.0, 4.0], [-1.5, 2.0], [0.0, -7.0]] {
assert(Math.hypot(x, y) == Math.hypot(y, x))
assert(Math.hypot(x, y) == Math.hypot(-x, -y))
}With y zero it equals Math.abs:
for x in [2.5, -2.5, 0.0, 1e300] {
assert(Math.hypot(x, 0.0) == Math.abs(x))
}On a Pythagorean triple the result is exact:
for [a, b, c] in [[3.0, 4.0, 5.0], [5.0, 12.0, 13.0], [8.0, 15.0, 17.0], [20.0, 21.0, 29.0]] {
assert(Math.hypot(a, b) == c)
}Possible issues
The written-out formula overflows where Math.hypot does not:
let x = 1e200
Math.sqrt(x * x + x * x) // Infinity
Math.hypot(x, x) // 1.414213562373095e+200It underflows to zero for small arguments too:
let x = 1e-200
Math.sqrt(x * x + x * x) // 0.0
Math.hypot(x, x) // 1.414213562373095e-200The arguments are float64. An int is a compile error; convert it first:
let dx = 3
let dy = 4
let d = Math.hypot(dx, dy)
// error: cannot use int where float64 is expectedlet dx = 3
let dy = 4
Math.hypot(float64(dx), float64(dy)) // 5.0A number has no namespace, so a call through . is a compile error. Write Math.hypot(x, y), or use a pipe:
let x = 3.0
let d = x.hypot(4.0) // error: float64 has no field or function hypotSee also
Math.sqrt · Math.abs · Math.atan2 · Math.pow · Math.cbrt · Math namespace
