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
💡 Examples
1. Basic Squaring:
2. Calculating Binary Steps:
Often used in networking or memory calculations.
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. |
🔗 Related Functions
-
[[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.