Sqrt()

Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Jul 18, 2023Updated Jul 28, 2023
Contribute to Docs

The Sqrt() function returns the square root of a given number.

Syntax

import math

sqrt = math.Sqrt(x)

Sqrt() accepts an argument of type float64 and returns a value of type float64.

Note these special cases:

  • The result of calling Sqrt() with a negative number is NaN.
  • If 0 is passed as the argument the result is 0.
  • Passing +Inf to Sqrt() results in +Inf.
  • If Sqrt() is called with NaN as the argument the result is Nan.

Example

package main
import (
"fmt"
"math"
)
func main() {
fmt.Printf("Square root of 9 = %f\n", math.Sqrt(9))
fmt.Printf("Square root of 12.5 = %f\n", math.Sqrt(12.5))
}

The above code results in the following output:

Square root of 9 = 3.000000
Square root of 12.5 = 3.535534

Codebyte Example

The runnable example demonstrates the output of Sqrt() in the special cases where 0 or a negative number are supplied as arguments.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Go on Codecademy