Golang math pow: power in Go with math.Pow

golang math pow and go math.pow with math.Pow(x,y float64); golang power examples, integer casts and overflow pitfalls, special cases from pkg.go.dev, and alternatives for exact integer exponentiation.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang math pow: power in Go with math.Pow

People searching golang power, golang math pow, math.pow golang, or go math.pow usually want one thing: raise a number to an exponent in Go. The standard answer is math.Pow, which implements x to the y for float64 inputs and result. This page shows a correct golang math pow example, explains why int arguments do not compile without a cast, and calls out overflow and NaN rules so power in golang code matches what the math package actually guarantees. For converting numeric types in general, see type casts in Go.

Checked with Go 1.24 on 64-bit Linux (Fedora and Ubuntu family).


golang math.pow: math.Pow for x to the y

Signature and meaning

go
func Pow(x, y float64) float64

math.Pow returns x raised to the power y. Both parameters are float64; there is no overload for int. The implementation follows floating-point rules (including infinities and NaNs); the full special-case table lives in the package documentation. Notable cases: any finite x with exponent ±0 yields 1; a finite negative x with a finite non-integer y yields NaN because no real result exists.

golang math pow examples

go
package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Pow(2, 9))    // 512
	fmt.Println(math.Pow(4.5, 2))  // 20.25
	fmt.Println(math.Pow(1520, 0)) // 1
}
Output

You should see three lines: 512, 20.25, and 1.

go math pow with integer operands

This does not compile because math.Pow expects float64:

text
cannot use a (variable of type int) as type float64 in argument to math.Pow

Typical fix: convert, compute, then convert back if you truly need an int result:

go
package main

import (
	"fmt"
	"math"
)

func main() {
	a, b := 4, 10
	fmt.Println(int(math.Pow(float64(a), float64(b)))) // 1048576
}
Output

That pattern is only safe when the mathematical result fits exactly in a float64 mantissa and in the int range; large exponents quickly overflow or round wrong. For nonnegative integer exponents and exact integers, exponentiation by squaring stays in integer arithmetic:

go
package main

import "fmt"

func intPow(x, n int) int {
	if n < 0 {
		panic("example assumes n >= 0")
	}
	if n == 0 {
		return 1
	}
	if n == 1 {
		return x
	}
	half := intPow(x, n/2)
	if n%2 == 0 {
		return half * half
	}
	return x * half * half
}

func main() {
	fmt.Println(intPow(4, 10))
}
Output

For very large values use math/big.

golang math power helpers

For 10 raised to an int exponent as float64, math.Pow10 is a readable shortcut where your Go version documents it. For powers of two with small nonnegative exponents, a left shift 1 << k computes two to the power k in integer math without calling Pow.


Summary

Golang math power for floating-point bases and exponents is normally math.Pow, which matches searches for golang math.pow, math pow golang, and go math.pow. Arguments must be float64; casting int values fixes the compile error but can hide precision loss and overflow, so prefer integer algorithms or math/big when the result must be exact. Read the pkg.go.dev edge-case list whenever inputs can be zero, negative, or fractional, because NaN and infinities follow IEEE rules, not grade-school algebra alone.


References


Frequently Asked Questions

1. What is golang math.pow?

It is the exported function math.Pow in the standard math package: func Pow(x, y float64) float64. It returns x raised to y for floating-point values, with edge cases defined in the official docs.

2. Why will go math pow not compile with int arguments?

Pow only accepts float64. Convert with float64(x) and float64(y), or use integer algorithms or math/big when you need exact integer math.

3. Is int(math.Pow(float64(a), float64(b))) safe for large results?

No. float64 has limited precision and int conversion truncates toward zero; large true integer powers overflow or round incorrectly. Use a loop, exponentiation by squaring, or math/big.Int.Exp for big values.

4. Does Go have a ** operator for power like Python?

No. Use math.Pow for floats, explicit multiplication in a loop for small integer exponents, bit shifts for powers of two when appropriate, or big integers for cryptography-sized numbers.

5. When does math.Pow return NaN?

Among other cases, Pow returns NaN if either argument is NaN, or if x is finite and negative while y is finite and not an integer—there is no real result for that combination.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …