Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Time, Math and Randomness

Go math.Sqrt() and math.Pow() functions

Reference for Go math.Sqrt, Cbrt, Pow, Hypot and Exp: signatures, special cases for negative and zero inputs, and how to compute integer powers exactly.

math.Sqrt(x) returns the square root of a float64, and math.Pow(x, y) returns x raised to the power y. Both live in the package math, both work on float64 only, and both follow the IEEE 754 rules for special values instead of panicking.

Square root and cube root (math.Cbrt) are computed exactly rounded or very nearly so. Pow handles fractional and negative exponents, which makes it more general than a loop of multiplications, but it returns a float, so integer results can pick up tiny errors for large values.

Go
fmt.Println(math.Sqrt(2))   // 1.4142135623730951
fmt.Println(math.Pow(2, 10)) // 1024

Try it

Go Roots and powers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Special cases and integer powers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func Sqrt(x float64) float64
func Cbrt(x float64) float64
func Pow(x, y float64) float64
func Pow10(n int) float64
func Hypot(p, q float64) float64
func Exp(x float64) float64
Name Description
x The number (base for Pow).
y The exponent for Pow.
n Integer exponent of 10 for Pow10.

Returns: a float64. There is no error value.

Special cases:

Call Result
Sqrt(x) for x < 0 NaN
Sqrt(+Inf) +Inf
Pow(x, 0) for any x 1
Pow(0, y) for y < 0 +Inf
Pow(x, y) for x < 0 and non-integer y NaN
Cbrt(x) for negative x A negative real root

Usage notes

  • No integer versions. math.Sqrt(float64(n)) needs a conversion, and the result is a float. For the integer square root of n, int(math.Sqrt(float64(n))) is fine for small values but can be off by one near 2^53; for exact results use big.Int.Sqrt, see math/big.
  • Negative bases: math.Pow(-8, 1.0/3) is NaN because the exponent is not an integer. Use Cbrt for real cube roots of negative numbers.
  • Exactness: Pow of small integers is usually exact, but you cannot rely on it for large results. Repeated squaring, as in the example, is exact for integers and runs in O(log exp) multiplications. Integer multiplication wraps around silently on overflow.
  • Hypot avoids overflow and loses less precision than Sqrt(p*p + q*q) for very large or very small values.
  • Performance: Sqrt compiles to a single CPU instruction on most platforms, so it is fast. Pow is much slower; for squaring, write x * x.
  • Comparing results: because of rounding, compare with a tolerance; see math.
  • Complex numbers: use math/cmplx (cmplx.Sqrt, cmplx.Pow) when negative roots should be imaginary.