Math

2 min read

Arithmetic on values that may arrive as integers, floats or numeric strings: add, subtract, multiply, divide, modulo, absolute value, minimum, maximum and clamping. Division and modulo by zero are errors.

Reference

FunctionDescription
absReturns the absolute value of a number.
addAdds two numbers.
clampConstrains a value to the range [lo, hi].
divDivides the first number by the second.
maxReturns the larger of two numbers.
minReturns the smaller of two numbers.
modReturns the remainder of dividing the first number by the second, as an integer (int64).
mulMultiplies two numbers.
subSubtracts the second number from the first.

Functions

abs

abs returns the absolute value of a number.

{{ abs -300 }}  → 300

add

add adds two numbers. Returns int64 when the result is whole, float64 otherwise.

{{ add 8080 1000 }}  → 9080

clamp

clamp constrains a value to the range [lo, hi]. Equivalent to max(lo, min(hi, val)).

{{ clamp 25 1 20 }}  → 20
{{ clamp 0 1 20 }}   → 1

div

div divides the first number by the second. Returns an error on division by zero.

{{ div 8 4 }}  → 2

max

max returns the larger of two numbers. Use to enforce a minimum floor.

{{ max .deployment.spec.replicas 5 }}  → 5

min

min returns the smaller of two numbers. Use to cap a value at an upper bound.

{{ min .deployment.spec.replicas 10 }}  → 3

mod

mod returns the remainder of dividing the first number by the second, as an integer (int64). An error when the second is zero.

{{ mod 8080 100 }}  → 80

mul

mul multiplies two numbers.

{{ mul .deployment.spec.replicas 2 }}  → 6

sub

sub subtracts the second number from the first.

{{ sub .deployment.spec.replicas 1 }}  → 2