Go Sqrt()

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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours

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.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn how to use Go (Golang), an open-source programming language supported by Google!
    • Beginner Friendly.
      6 hours