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 .
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.
The definition of the Hypot
function inside the math
package is:
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.
The Hypot
function returns a single value of type float64
, which represents the hypotenuse resulting from the formula .
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.
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 mainimport ("fmt""math")func main() {x := 43.76y := 63.85z := 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 mainimport ("fmt""math")func main() {x := 43.76y := 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 mainimport ("fmt""math")func main() {x := 43.76y := math.NaN()z := math.Hypot(x, y)fmt.Println("Hypotenues of the triangle with sides",x,"and",y,"is", z)}
Free Resources