×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang math.Hypot() Function with Examples

Golang | math.Hypot() Function: Here, we are going to learn about the Hypot() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 01, 2021

math.Hypot()

The Hypot() function is an inbuilt function of the math package which is used to get the square root of the sum of squares of the given parameters (Sqrt(p*p + q*q)), taking care to avoid unnecessary overflow and underflow.

It accepts two a parameter (p, q), and returns the Sqrt(p*p + q*q).

Syntax

func Hypot(p, q float64) float64

Parameters

  • p, q : The values whose square root of the sum of squares is to be returned.

Return Value

The return type of Hypot() function is a float64, it returns the Sqrt(p*p + q*q).

Special Cases

  • Hypot(±Inf, q) = +Inf
    If the first parameter is ±Inf, the fruition returns the positive infinity (+Inf).
  • Hypot(p, ±Inf) = +Inf
    If the second parameter is ±Inf, the function returns the positive infinity (+Inf).
  • Hypot(NaN, q) = NaN
    If the first parameter is NaN, the function returns the NaN.
  • Hypot(p, NaN) = NaN
    If the second parameter NaN, the function returns the NaN.

Example 1

// Golang program to demonstrate the
// example of math.Hypot() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Hypot(0, 0))
	fmt.Println(math.Hypot(1, 1))
	fmt.Println(math.Hypot(10, 20))
	fmt.Println(math.Hypot(-1, 2))
	fmt.Println(math.Hypot(-10.50, 20.50))
	fmt.Println(math.Hypot(10.50, -20.50))
	fmt.Println(math.Hypot(-10.50, -20.50))

	fmt.Println(math.Hypot(1, math.Inf(1)))
	fmt.Println(math.Hypot(math.Inf(1), 1))
	fmt.Println(math.Hypot(1, math.NaN()))
	fmt.Println(math.Hypot(math.NaN(), 1))
}

Output:

0
1.4142135623730951
22.360679774997898
2.23606797749979
23.03258561256204
23.03258561256204
23.03258561256204
+Inf
+Inf
NaN
NaN

Example 2

// Golang program to demonstrate the
// example of math.Hypot() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var HypotXY float64

	x = 0
	y = 1
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)

	x = 1
	y = 2
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)

	x = -1.49
	y = -2.5
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)

	x = 40
	y = 60
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)

	x = 40
	y = math.Inf(-1)
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)

	x = math.NaN()
	y = 60
	HypotXY = math.Hypot(x, y)
	fmt.Println("Hypot(", x, ",", y, ") = ", HypotXY)
}

Output:

Hypot( 0 , 1 ) =  1
Hypot( 1 , 2 ) =  2.23606797749979
Hypot( -1.49 , -2.5 ) =  2.9103436223236594
Hypot( 40 , 60 ) =  72.11102550927978
Hypot( 40 , -Inf ) =  +Inf
Hypot( NaN , 60 ) =  NaN

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.