Skip to content

Numeric Functions

Numeric functions provide mathematical operations, conversions, and number formatting capabilities in HCL expressions.

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.5
abs(42) # Returns: 42
abs(-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: 11
ceil(10.9) # Returns: 11
ceil(10) # Returns: 10
ceil(-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: 10
floor(10.1) # Returns: 10
floor(10) # Returns: 10
floor(-5.5) # Returns: -6

Calculates the logarithm in a specified base.

log(number, base)

Parameters:

  • number - Number to calculate logarithm of
  • base - 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: 5
max(-10, -5, -20) # Returns: -5
max(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: 1
min(-10, -5, -20) # Returns: -20
min(3.14, 2.71, 3.5) # Returns: 2.71

Parses a string as an integer in the specified base.

parseint(string, base)

Parameters:

  • string - String representation of a number
  • base - 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 number
  • exponent - 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)

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: -1
signum(0) # Returns: 0
signum(42) # Returns: 1
signum(-0.01) # Returns: -1
variable "base_port" {
default = 8000
}
resource "container" "web" {
count = 3
port {
local = local.base_port + count.index
host = local.base_port + count.index
}
}
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)
}
local {
# Calculate 80% of available resources
allocated_cpu = floor(var.total_cpu * 0.8)
# Convert percentage to decimal
scale_factor = var.scale_percentage / 100
}
local {
# Parse binary flags
flags = parseint(var.binary_flags, 2)
# Calculate powers of 2 for bit positions
bit_mask = pow(2, var.bit_position)
}
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)
}
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)
}
  1. Handle Division by Zero: Check denominators before division

    local {
    safe_average = var.count > 0 ? var.total / var.count : 0
    }
  2. Use Appropriate Rounding: Choose ceil/floor based on use case

    local {
    # Round up for resource allocation
    required_nodes = ceil(var.total_pods / var.pods_per_node)
    # Round down for distribution
    items_per_group = floor(var.total_items / var.group_count)
    }
  3. Validate Numeric Inputs: Ensure values are within expected ranges

    local {
    # Ensure positive port numbers
    port = max(abs(var.port), 1024)
    # Limit percentage to 0-100
    percentage = min(max(var.percentage, 0), 100)
    }
  4. Consider Precision: Be aware of floating-point limitations

    local {
    # Use integers for precise calculations when possible
    cents = var.dollars * 100
    final_dollars = local.cents / 100
    }