Numeric Function
Numeric functions are useful when one needs to perform mathematical operations on numbers in Terraform configurations. For instance, one may want to find a maximum or minimum value of some set of numbers, or even round a number to the nearest integer.
Types of Numeric Functions
Terraform provides the following types of numeric functions:
1. ceil Function
The ceil function in Terraform returns the nearest whole number that is greater than or equal to the given value. Even if an input is fractional, the ceil will always return the next whole number. Here how it works for the ceil function:
- If the input value is an integer, then the ceil function returns the number itself.
- If a fraction is taken as an input value, then the ceil function returns the next whole number that is greater than or equal to the input value.
For example
terraform console
> ceil(4)
4
> ceil(4.1)
5
> ceil(4.5)
5
> ceil(4.0)
4
2. floor Function
The Terraform floor function returns the nearest whole number that is less than or equal to a given value. Here is how the floor function works:
- The floor function returns the same number as it is, if the input number already is a whole number.
- if the input value is a fraction, then the floor function returns the next whole number less than or equal to the input value.
For example
terraform console
> floor(4)
4
> floor(4.1)
4
> floor(4.5)
4
> floor(4.0)
4
3. log Function
The log function calculates the logarithm of a given number in a specified base using the Terraform configuration.
For example
terraform console
> log(100, 10)
2
> log(8, 2)
3
> log(1000, 10)
2.9999999999999996
> log(16, 2)
4
4. max Function
For example
terraform console
> max(2, 102, 9)
102
> max([2, 102, 9]...)
102
5. min Function
For example
terraform console
> min(2, 102, 9)
2
> min([2, 102, 9]...)
2
6. parseint Function
The parseint from Terraform will convert a given string into an integer with a specified base. A base ranging between 2 and 62 (inclusive).
If the input string contains any non-digit characters or digit characters which are too large for the given base, the parseint function will throw an error.
For example
terraform console
> min(2, 102, 9)
2
> min([2, 102, 9]...)
2
7. pow Function
The pow function in Terraform returns an exponent by raising its first argument (the base) to the power of its second argument, the exponent.
For example
terraform console
> pow(4, 5)
1024
8. signum Function
The signum function in Terraform returns the sign of a given number. It returns a number that ranges between -1 and 1 representing the sign. Well, here's how the signum function should work:
- The function returns 1, if a number is positive.
- If the input number is negative, the function returns -1.
- The function returns 0 if the input number is zero.
For example
terraform console
> signum(-30)
-1
> signum(30)
1
> signum(0)
0
9. abs Function
The abs function in Terraform returns the absolute value of the given number. In simple words, the absolute value of a number can be defined as the distance of that number from zero on the number line being absolutely irrespective of its sign. Here is how the ABS function works:
- If the input number is zero or positive, the abs returns the same input number.
- If the input number is negative, the abs function multiplies it by -1 to make it positive before returning it.
For example
terraform console
> abs(5)
5
> abs(-15)
15
> abs(0)
0
Related Pages
Feedback
Was this page helpful?