What is the Hypot function in Golang?

The Go programming language uses the Hypot function to find the hypotenuse, given the relevant lengths of the other two sides of a right-angle triangle. This is calculated using the formula p2+q2\sqrt{p^{2} + q^{2} }.

To use this function, you must import the math package in your file and access the Hypot function within it using the . notation (math.Hypot). Here, Hypot is the actual function, while math is the Go package that stores the definition of this function.

Function definition

The definition of the Hypot function inside the math package is:

Parameters

The Hypot function takes two arguments of type float64. These arguments represent the two sides (the base and the perpendicular) of a right-angle triangle whose hypotenuse is to be calculated.

Return value

The Hypot function returns a single value of type float64, which represents the hypotenuse resulting from the formula p2+q2\sqrt{p^{2} + q^{2} }.

Exceptions to the above are:

  • The Hypot function returns NAN when either one or both of the arguments passed are NAN values.

  • The Hypot function returns +Inf when either one or both of the arguments passed are positive/negative infinite values.

Examples

Following is a simple example where we use the Hypot function to find the hypotenuse of a right-angle triangle using the provided lengths of the other two sides.

package main
import (
"fmt"
"math"
)
func main() {
x := 43.76
y := 63.85
z := math.Hypot(x, y)
fmt.Println("Hypotenues of the triangle with sides",x,"and",y,"is", z)
}

The following example shows how the Hypot function handles infinite value arguments.

The Inf function returns an infinite value with a sign matching the sign of the argument that it is given.

package main
import (
"fmt"
"math"
)
func main() {
x := 43.76
y := math.Inf(+1)
z := math.Hypot(x, y)
fmt.Println("Hypotenues of the triangle with sides",x,"and",y,"is", z)
}

The following example shows how the Hypot function handles undefined values.

We use the NaN function to generate the undefined numeric values to test.

package main
import (
"fmt"
"math"
)
func main() {
x := 43.76
y := math.NaN()
z := math.Hypot(x, y)
fmt.Println("Hypotenues of the triangle with sides",x,"and",y,"is", z)
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved