Go 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
0is passed as the argument the result is0. - Passing
+InftoSqrt()results in+Inf. - If
Sqrt()is called withNaNas 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
- 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