Hypot()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 22, 2023
Contribute to Docs

The Hypot() function returns the square root of the sum of two squares. In other words, the method returns the result of the following formula:

hypotenuse=(xx)+(yy)hypotenuse = \sqrt{(x * x) + (y * y)}

Syntax

import math

result := math.Hypot(x, y)

Hypot() accepts two arguments (x , y) of type float64 and returns a value of type float64.

Note:

  • If either argument is negative, or both, the result is positive.
  • If either argument is positive infinity (+Inf), the result will be positive infinity.
  • If 0 is passed as both arguments, the result is 0. If one of the arguments is 0 the result is equal to the other argument.
  • If either argument passed is NaN, the result will be NaN.

Example

package main
import("fmt"
"math")
func main() {
fmt.Printf("Hypotenuse of 3 and 4 = %f\n", math.Hypot(3 , 4))
fmt.Printf("Hypotenuse of -1.5 and -1.5 = %f\n", math.Hypot(-1.5,-1.5))
}

The above example results in the following output:

Hypotenuse of 3 and 4 = 5.000000
Hypotenuse of -1.5 and -1.5 = 2.121320

Codebyte Example

The runnable example of the Hypot() function of the cases where 0 or a negative number is at least one of the arguments.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy