Sqrt()
function is used to find the square root of the given input (x
– parameter) in Go language. The standard math package of Go programming language has Sqrt()
function.
Syntax of Sqrt()
Function in Go Language
The syntax of Sqrt()
function in Go Language is:
1 | func Sqrt(x float64) float64 |
Note: float64
is a data type in Go language which has IEEE-754 64-bit floating-point numbers.
Special cases are:
Sqrt(+Inf) = +Inf
Sqrt(±0) = ±0
Sqrt(x < 0) = NaN
Sqrt(NaN) = NaN
Parameters of Sqrt()
Function in Go Language
x
– where x
is any Valid float64
Input value. This parameter is required.
Error Handling
If the
If there is no argument (
x
parameter is not a number (numeric value) Sqrt()
function returns an error
.If there is no argument (
x
– input value) passes to the function, then the compiler will produce an error
.
Return Value of Sqrt()
Function in GoLang
Sqrt()
function will return the square root of the given input(x
– parameter).
GoLang Sqrt()
Function Example 1
1 2 3 4 5 6 7 8 9 10 | package main import "fmt" import "math" func main() { var x float64 x = math.Sqrt(-2.4) fmt.Println(x) } |
Output:
NaN
GoLang Sqrt()
Function Example 2
1 2 3 4 5 6 7 8 9 10 | package main import "fmt" import "math" func main() { var x float64 x = math.Sqrt(5.4) fmt.Println(x) } |
Output:
2.32379000772445
GoLang Sqrt()
Function Example 3
1 2 3 4 5 6 7 8 9 10 | package main import "fmt" import "math" func main() { var x float64 x = math.Sqrt(4) fmt.Println(x) } |
Output:
2