Skip to content

Function pow()

📝 Description

The pow function raises a base to the power of an exponent. It calculates $base^{exponent}$.

Note: If the result is a whole number, it returns an integer. If the result is fractional, it returns a float.


💻 Syntax

pow(base, exponent)

💡 Examples

1. Basic Squaring:

# Result: 16 (4 * 4)
output "squared" {
  value = pow(4, 2)
}

2. Calculating Binary Steps:

Often used in networking or memory calculations.

# Result: 1024 (2 to the power of 10)
output "kb_in_bytes" {
  value = pow(2, 10)
}

3. Capacity Scaling (Real-world scenario):

If you want to define disk sizes that grow exponentially based on an index:

locals {
  base_size = 10
  # If index is 3: 10 * (2^3) = 80GB
  disk_size = local.base_size * pow(2, var.index) 
}

⚠️ Common Errors

Input Result Why?
pow(-4, 0.5) Error You cannot take the square root (0.5 power) of a negative number (imaginary numbers are not supported).
pow("2", 3) Success Terraform will try to convert the string "2" to a number automatically, but it's better to pass numbers directly.

  • [[log]] — The inverse of pow: calculates the logarithm.

  • [[abs]] — Useful if you need to ensure the base is positive before raising it to a fractional power.

  • Official HashiCorp Docs