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:
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:
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

The Terraform max function takes one or more numbers and returns the greatest number from that set. If you have a list or set of numbers to pass into the max function, you can use the ... syntax to expand the collection into individual arguments.
For example
terraform console
> max(2, 102, 9)
102
> max([2, 102, 9]...)
102

5. min Function

The Terraform min function takes one or more numbers and returns the smallest number out of the set. If you have a list or set of numbers that you want to pass into the min function, you can use the ... syntax to expand the collection into individual arguments.
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:
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:
For example
terraform console
> abs(5)
5
> abs(-15)
15
> abs(0)
0

Related Pages

Feedback

Was this page helpful?