Sqrt()
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 isNaN
. - If
0
is passed as the argument the result is0
. - Passing
+Inf
toSqrt()
results in+Inf
. - If
Sqrt()
is called withNaN
as the argument the result isNan
.
Example
package mainimport ("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.000000Square 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.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours