math functions
A set of basic math functions to be able to perform simple arithmetic operations with gomplate.
Supported input
In general, any input will be converted to the correct input type by the various functions in this package, and an appropriately-typed value will be returned type. Special cases are documented.
In addition to regular base-10 numbers, integers can be
specified as octal (prefix with
0) or hexadecimal (prefix with 0x).
Decimal/floating-point numbers can be specified with optional exponents.
Some examples demonstrating this:
math.Abs
Returns the absolute value of a given number. When the input is an integer, the result will be an int64, otherwise it will be a float64.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The input number |
Examples
math.Add
Alias: add
Adds all given operators. When one of the inputs is a floating-point number, the result will be a float64, otherwise it will be an int64.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
n... |
(required) The numbers to add together |
Examples
math.Ceil
Returns the least integer value greater than or equal to a given floating-point number. This wraps Go’s math.Ceil.
Note: the return value of this function is a float64 so that the special-cases NaN and Inf can be returned appropriately.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The input number. Will be converted to a float64, or 0 if not convertible |
Examples
math.Div
Alias: div
Divide the first number by the second. Division by zero is disallowed. The result will be a float64.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
a |
(required) The divisor |
b |
(required) The dividend |
Examples
math.Floor
Returns the greatest integer value less than or equal to a given floating-point number. This wraps Go’s math.Floor.
Note: the return value of this function is a float64 so that the special-cases NaN and Inf can be returned appropriately.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The input number. Will be converted to a float64, or 0 if not convertible |
Examples
math.IsFloat
Returns whether or not the given number can be interpreted as a floating-point literal, as defined by the Go language reference.
Note: If a decimal point is part of the input number, it will be considered a floating-point number, even if the decimal is 0.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The value to test |
Examples
math.IsInt
Returns whether or not the given number is an integer.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The value to test |
Examples
math.IsNum
Returns whether the given input is a number. Useful for if conditions.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
in |
(required) The value to test |
Examples
math.Max
Returns the largest number provided. If any values are floating-point numbers, a float64 is returned, otherwise an int64 is returned. The same special-cases as Go’s math.Max are followed.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
nums... |
(required) One or more numbers to compare |
Examples
math.Min
Returns the smallest number provided. If any values are floating-point numbers, a float64 is returned, otherwise an int64 is returned. The same special-cases as Go’s math.Min are followed.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
nums... |
(required) One or more numbers to compare |
Examples
math.Mul
Alias: mul
Multiply all given operators together.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
n... |
(required) The numbers to multiply |
Examples
math.Pow
Alias: pow
Calculate an exponent - bn. This wraps Go’s math.Pow. If any values are floating-point numbers, a float64 is returned, otherwise an int64 is returned.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
b |
(required) The base |
n |
(required) The exponent |
Examples
math.Rem
Alias: rem
Return the remainder from an integer division operation.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
a |
(required) The divisor |
b |
(required) The dividend |
Examples
math.Round
Returns the nearest integer, rounding half away from zero.
Note: the return value of this function is a float64 so that the special-cases NaN and Inf can be returned appropriately.
Added in gomplate v2.6.0
Usage
Arguments
| name | description |
|---|---|
num |
(required) The input number. Will be converted to a float64, or 0 if not convertible |
Examples
math.Seq
Alias: seq
Return a sequence from start to end, in steps of step. Can handle counting
down as well as up, including with negative numbers.
Note that the sequence may not end at end, if end is not divisible by step.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
start |
(optional) The first number in the sequence (defaults to 1) |
end |
(required) The last number in the sequence |
step |
(optional) The amount to increment between each number (defaults to 1) |
Examples
math.Sub
Alias: sub
Subtract the second from the first of the given operators. When one of the inputs is a floating-point number, the result will be a float64, otherwise it will be an int64.
Added in gomplate v2.2.0
Usage
Arguments
| name | description |
|---|---|
a |
(required) The minuend (the number to subtract from) |
b |
(required) The subtrahend (the number being subtracted) |