Numeric Functions
Numeric functions provide mathematical operations, conversions, and number formatting capabilities in HCL expressions.
Function Reference
Section titled “Function Reference”Returns the absolute value of a number.
abs(number)
Parameters:
number
- Number to process
Returns: Absolute value (always positive)
Example:
abs(-10.5) # Returns: 10.5abs(42) # Returns: 42abs(-0) # Returns: 0
Rounds up to the nearest whole number.
ceil(number)
Parameters:
number
- Number to round up
Returns: Smallest integer greater than or equal to the input
Example:
ceil(10.1) # Returns: 11ceil(10.9) # Returns: 11ceil(10) # Returns: 10ceil(-5.5) # Returns: -5
Rounds down to the nearest whole number.
floor(number)
Parameters:
number
- Number to round down
Returns: Largest integer less than or equal to the input
Example:
floor(10.9) # Returns: 10floor(10.1) # Returns: 10floor(10) # Returns: 10floor(-5.5) # Returns: -6
Calculates the logarithm in a specified base.
log(number, base)
Parameters:
number
- Number to calculate logarithm ofbase
- Logarithm base
Returns: Logarithm value
Example:
log(16, 2) # Returns: 4 (2^4 = 16)log(1000, 10) # Returns: 3 (10^3 = 1000)log(27, 3) # Returns: 3 (3^3 = 27)
Returns the largest value from the arguments.
max(numbers...)
Parameters:
numbers...
- One or more numbers to compare
Returns: Maximum value
Example:
max(1, 5, 3) # Returns: 5max(-10, -5, -20) # Returns: -5max(3.14, 2.71, 3.5) # Returns: 3.5
Returns the smallest value from the arguments.
min(numbers...)
Parameters:
numbers...
- One or more numbers to compare
Returns: Minimum value
Example:
min(1, 5, 3) # Returns: 1min(-10, -5, -20) # Returns: -20min(3.14, 2.71, 3.5) # Returns: 2.71
parseint
Section titled “parseint”Parses a string as an integer in the specified base.
parseint(string, base)
Parameters:
string
- String representation of a numberbase
- Number base (2-62)
Returns: Parsed integer
Example:
parseint("100", 10) # Returns: 100 (decimal)parseint("100", 2) # Returns: 4 (binary: 100 = 4)parseint("FF", 16) # Returns: 255 (hexadecimal)parseint("77", 8) # Returns: 63 (octal)
Calculates exponentiation (power).
pow(base, exponent)
Parameters:
base
- Base numberexponent
- Power to raise to
Returns: Result of base^exponent
Example:
pow(2, 3) # Returns: 8 (2^3)pow(10, 2) # Returns: 100 (10^2)pow(5, 0) # Returns: 1 (any number^0)pow(2, -2) # Returns: 0.25 (1/4)
signum
Section titled “signum”Determines the sign of a number.
signum(number)
Parameters:
number
- Number to check
Returns: -1 for negative, 0 for zero, 1 for positive
Example:
signum(-42) # Returns: -1signum(0) # Returns: 0signum(42) # Returns: 1signum(-0.01) # Returns: -1
Common Use Cases
Section titled “Common Use Cases”Port Number Calculations
Section titled “Port Number Calculations”variable "base_port" { default = 8000}
resource "container" "web" { count = 3
port { local = local.base_port + count.index host = local.base_port + count.index }}
Resource Scaling
Section titled “Resource Scaling”local { # Scale CPU based on environment (rounded up) cpu_count = ceil(var.is_production ? 4 : 1.5)
# Memory in MB (ensure minimum of 512) memory = max(var.requested_memory, 512)}
Percentage Calculations
Section titled “Percentage Calculations”local { # Calculate 80% of available resources allocated_cpu = floor(var.total_cpu * 0.8)
# Convert percentage to decimal scale_factor = var.scale_percentage / 100}
Binary Operations
Section titled “Binary Operations”local { # Parse binary flags flags = parseint(var.binary_flags, 2)
# Calculate powers of 2 for bit positions bit_mask = pow(2, var.bit_position)}
Validation and Bounds
Section titled “Validation and Bounds”variable "replicas" { type = number validation { condition = var.replicas >= 1 && var.replicas <= 10 error_message = "Replicas must be between 1 and 10" }}
local { # Ensure value is within bounds safe_replicas = min(max(var.replicas, 1), 10)}
Mathematical Formulas
Section titled “Mathematical Formulas”local { # Calculate area of a circle (πr²) circle_area = 3.14159 * pow(var.radius, 2)
# Convert Celsius to Fahrenheit fahrenheit = (var.celsius * 9/5) + 32
# Calculate compound interest final_amount = var.principal * pow(1 + var.rate, var.years)}
Best Practices
Section titled “Best Practices”-
Handle Division by Zero: Check denominators before division
local {safe_average = var.count > 0 ? var.total / var.count : 0} -
Use Appropriate Rounding: Choose ceil/floor based on use case
local {# Round up for resource allocationrequired_nodes = ceil(var.total_pods / var.pods_per_node)# Round down for distributionitems_per_group = floor(var.total_items / var.group_count)} -
Validate Numeric Inputs: Ensure values are within expected ranges
local {# Ensure positive port numbersport = max(abs(var.port), 1024)# Limit percentage to 0-100percentage = min(max(var.percentage, 0), 100)} -
Consider Precision: Be aware of floating-point limitations
local {# Use integers for precise calculations when possiblecents = var.dollars * 100final_dollars = local.cents / 100}