github.com/wuhuizuo/gomplate@v3.5.0+incompatible/docs/content/functions/math.md (about)

     1  ---
     2  title: math functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  A set of basic math functions to be able to perform simple arithmetic operations with `gomplate`.
     9  
    10  ### Supported input
    11  
    12  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.
    13  
    14  In addition to regular base-10 numbers, integers can be
    15  [specified](https://golang.org/ref/spec#Integer_literals) as octal (prefix with
    16  `0`) or hexadecimal (prefix with `0x`).
    17  
    18  Decimal/floating-point numbers can be [specified](https://golang.org/ref/spec#Floating-point_literals)
    19  with optional exponents.
    20  
    21  Some examples demonstrating this:
    22  
    23  ```console
    24  $ NUM=50 gomplate -i '{{ div (getenv "NUM") 10 }}'
    25  5
    26  $ gomplate -i '{{ add "0x2" "02" "2.0" "2e0" }}'
    27  8
    28  $ gomplate -i '{{ add 2.5 2.5 }}'
    29  5.0
    30  ```
    31  
    32  ## `math.Abs`
    33  
    34  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`.
    35  
    36  ### Usage
    37  
    38  ```go
    39  math.Abs num
    40  ```
    41  
    42  ### Arguments
    43  
    44  | name | description |
    45  |------|-------------|
    46  | `num` | _(required)_ The input number |
    47  
    48  ### Examples
    49  
    50  ```console
    51  $ gomplate -i '{{ math.Abs -3.5 }} {{ math.Abs 3.5 }} {{ math.Abs -42 }}'
    52  3.5 3.5 42
    53  ```
    54  
    55  ## `math.Add`
    56  
    57  **Alias:** `add`
    58  
    59  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`.
    60  
    61  ### Usage
    62  
    63  ```go
    64  math.Add n...
    65  ```
    66  
    67  ### Arguments
    68  
    69  | name | description |
    70  |------|-------------|
    71  | `n...` | _(required)_ The numbers to add together |
    72  
    73  ### Examples
    74  
    75  ```console
    76  $ gomplate -i '{{ math.Add 1 2 3 4 }} {{ math.Add 1.5 2 3 }}'
    77  10 6.5
    78  ```
    79  
    80  ## `math.Ceil`
    81  
    82  Returns the least integer value greater than or equal to a given floating-point number. This wraps Go's [`math.Ceil`](https://golang.org/pkg/math/#Ceil).
    83  
    84  **Note:** the return value of this function is a `float64` so that the special-cases `NaN` and `Inf` can be returned appropriately.
    85  
    86  ### Usage
    87  
    88  ```go
    89  math.Ceil num
    90  ```
    91  
    92  ### Arguments
    93  
    94  | name | description |
    95  |------|-------------|
    96  | `num` | _(required)_ The input number. Will be converted to a `float64`, or `0` if not convertible |
    97  
    98  ### Examples
    99  
   100  ```console
   101  $ gomplate -i '{{ range (slice 5.1 42 "3.14" "0xFF" "NaN" "Inf" "-0") }}ceil {{ printf "%#v" . }} = {{ math.Ceil . }}{{"\n"}}{{ end }}'
   102  ceil 5.1 = 6
   103  ceil 42 = 42
   104  ceil "3.14" = 4
   105  ceil "0xFF" = 255
   106  ceil "NaN" = NaN
   107  ceil "Inf" = +Inf
   108  ceil "-0" = 0
   109  ```
   110  
   111  ## `math.Div`
   112  
   113  **Alias:** `div`
   114  
   115  Divide the first number by the second. Division by zero is disallowed. The result will be a `float64`.
   116  
   117  ### Usage
   118  
   119  ```go
   120  math.Div a b
   121  ```
   122  ```go
   123  b | math.Div a
   124  ```
   125  
   126  ### Arguments
   127  
   128  | name | description |
   129  |------|-------------|
   130  | `a` | _(required)_ The divisor |
   131  | `b` | _(required)_ The dividend |
   132  
   133  ### Examples
   134  
   135  ```console
   136  $ gomplate -i '{{ math.Div 8 2 }} {{ math.Div 3 2 }}'
   137  4 1.5
   138  ```
   139  
   140  ## `math.Floor`
   141  
   142  Returns the greatest integer value less than or equal to a given floating-point number. This wraps Go's [`math.Floor`](https://golang.org/pkg/math/#Floor).
   143  
   144  **Note:** the return value of this function is a `float64` so that the special-cases `NaN` and `Inf` can be returned appropriately.
   145  
   146  ### Usage
   147  
   148  ```go
   149  math.Floor num
   150  ```
   151  
   152  ### Arguments
   153  
   154  | name | description |
   155  |------|-------------|
   156  | `num` | _(required)_ The input number. Will be converted to a `float64`, or `0` if not convertable |
   157  
   158  ### Examples
   159  
   160  ```console
   161  $ gomplate -i '{{ range (slice 5.1 42 "3.14" "0xFF" "NaN" "Inf" "-0") }}floor {{ printf "%#v" . }} = {{ math.Floor . }}{{"\n"}}{{ end }}'
   162  floor 5.1 = 4
   163  floor 42 = 42
   164  floor "3.14" = 3
   165  floor "0xFF" = 255
   166  floor "NaN" = NaN
   167  floor "Inf" = +Inf
   168  floor "-0" = 0
   169  ```
   170  
   171  ## `math.IsFloat`
   172  
   173  Returns whether or not the given number can be interpreted as a floating-point literal, as defined by the [Go language reference](https://golang.org/ref/spec#Floating-point_literals).
   174  
   175  **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`.
   176  
   177  ### Usage
   178  
   179  ```go
   180  math.IsFloat num
   181  ```
   182  
   183  ### Arguments
   184  
   185  | name | description |
   186  |------|-------------|
   187  | `num` | _(required)_ The value to test |
   188  
   189  ### Examples
   190  
   191  ```console
   192  $ gomplate -i '{{ range (slice 1.0 "-1.0" 5.1 42 "3.14" "foo" "0xFF" "NaN" "Inf" "-0") }}{{ if (math.IsFloat .) }}{{.}} is a float{{"\n"}}{{ end }}{{end}}'
   193  1 is a float
   194  -1.0 is a float
   195  5.1 is a float
   196  3.14 is a float
   197  NaN is a float
   198  Inf is a float
   199  ```
   200  
   201  ## `math.IsInt`
   202  
   203  Returns whether or not the given number is an integer.
   204  
   205  ### Usage
   206  
   207  ```go
   208  math.IsInt num
   209  ```
   210  
   211  ### Arguments
   212  
   213  | name | description |
   214  |------|-------------|
   215  | `num` | _(required)_ The value to test |
   216  
   217  ### Examples
   218  
   219  ```console
   220  $ gomplate -i '{{ range (slice 1.0 "-1.0" 5.1 42 "3.14" "foo" "0xFF" "NaN" "Inf" "-0") }}{{ if (math.IsInt .) }}{{.}} is an integer{{"\n"}}{{ end }}{{end}}'
   221  42 is an integer
   222  0xFF is an integer
   223  -0 is an integer
   224  ```
   225  
   226  ## `math.IsNum`
   227  
   228  Returns whether the given input is a number. Useful for `if` conditions.
   229  
   230  ### Usage
   231  
   232  ```go
   233  math.IsNum in
   234  ```
   235  
   236  ### Arguments
   237  
   238  | name | description |
   239  |------|-------------|
   240  | `in` | _(required)_ The value to test |
   241  
   242  ### Examples
   243  
   244  ```console
   245  $ gomplate -i '{{ math.IsNum "foo" }} {{ math.IsNum 0xDeadBeef }}'
   246  false true
   247  ```
   248  
   249  ## `math.Max`
   250  
   251  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`](https://golang.org/pkg/math/#Max) are followed.
   252  
   253  ### Usage
   254  
   255  ```go
   256  math.Max nums...
   257  ```
   258  
   259  ### Arguments
   260  
   261  | name | description |
   262  |------|-------------|
   263  | `nums...` | _(required)_ One or more numbers to compare |
   264  
   265  ### Examples
   266  
   267  ```console
   268  $ gomplate -i '{{ math.Max 0 8.0 4.5 "-1.5e-11" }}'
   269  8
   270  ```
   271  
   272  ## `math.Min`
   273  
   274  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`](https://golang.org/pkg/math/#Min) are followed.
   275  
   276  ### Usage
   277  
   278  ```go
   279  math.Min nums...
   280  ```
   281  
   282  ### Arguments
   283  
   284  | name | description |
   285  |------|-------------|
   286  | `nums...` | _(required)_ One or more numbers to compare |
   287  
   288  ### Examples
   289  
   290  ```console
   291  $ gomplate -i '{{ math.Min 0 8 4.5 "-1.5e-11" }}'
   292  -1.5e-11
   293  ```
   294  
   295  ## `math.Mul`
   296  
   297  **Alias:** `mul`
   298  
   299  Multiply all given operators together.
   300  
   301  ### Usage
   302  
   303  ```go
   304  math.Mul n...
   305  ```
   306  
   307  ### Arguments
   308  
   309  | name | description |
   310  |------|-------------|
   311  | `n...` | _(required)_ The numbers to multiply |
   312  
   313  ### Examples
   314  
   315  ```console
   316  $ gomplate -i '{{ math.Mul 8 8 2 }}'
   317  128
   318  ```
   319  
   320  ## `math.Pow`
   321  
   322  **Alias:** `pow`
   323  
   324  Calculate an exponent - _b<sup>n</sup>_. This wraps Go's [`math.Pow`](https://golang.org/pkg/math/#Pow). If any values are floating-point numbers, a `float64` is returned, otherwise an `int64` is returned.
   325  
   326  ### Usage
   327  
   328  ```go
   329  math.Pow b n
   330  ```
   331  
   332  ### Arguments
   333  
   334  | name | description |
   335  |------|-------------|
   336  | `b` | _(required)_ The base |
   337  | `n` | _(required)_ The exponent |
   338  
   339  ### Examples
   340  
   341  ```console
   342  $ gomplate -i '{{ math.Pow 10 2 }}'
   343  100
   344  $ gomplate -i '{{ math.Pow 2 32 }}'
   345  4294967296
   346  $ gomplate -i '{{ math.Pow 1.5 2 }}'
   347  2.2
   348  ```
   349  
   350  ## `math.Rem`
   351  
   352  **Alias:** `rem`
   353  
   354  Return the remainder from an integer division operation.
   355  
   356  ### Usage
   357  
   358  ```go
   359  math.Rem a b
   360  ```
   361  ```go
   362  b | math.Rem a
   363  ```
   364  
   365  ### Arguments
   366  
   367  | name | description |
   368  |------|-------------|
   369  | `a` | _(required)_ The divisor |
   370  | `b` | _(required)_ The dividend |
   371  
   372  ### Examples
   373  
   374  ```console
   375  $ gomplate -i '{{ math.Rem 5 3 }}'
   376  2
   377  $ gomplate -i '{{ math.Rem -5 3 }}'
   378  -2
   379  ```
   380  
   381  ## `math.Round`
   382  
   383  Returns the nearest integer, rounding half away from zero.
   384  
   385  **Note:** the return value of this function is a `float64` so that the special-cases `NaN` and `Inf` can be returned appropriately.
   386  
   387  ### Usage
   388  
   389  ```go
   390  math.Round num
   391  ```
   392  
   393  ### Arguments
   394  
   395  | name | description |
   396  |------|-------------|
   397  | `num` | _(required)_ The input number. Will be converted to a `float64`, or `0` if not convertable |
   398  
   399  ### Examples
   400  
   401  ```console
   402  $ gomplate -i '{{ range (slice -6.5 5.1 42.9 "3.5" 6.5) }}round {{ printf "%#v" . }} = {{ math.Round . }}{{"\n"}}{{ end }}'
   403  round -6.5 = -7
   404  round 5.1 = 5
   405  round 42.9 = 43
   406  round "3.5" = 4
   407  round 6.5 = 7
   408  ```
   409  
   410  ## `math.Seq`
   411  
   412  **Alias:** `seq`
   413  
   414  Return a sequence from `start` to `end`, in steps of `step`. Can handle counting
   415  down as well as up, including with negative numbers.
   416  
   417  Note that the sequence _may_ not end at `end`, if `end` is not divisible by `step`.
   418  
   419  ### Usage
   420  
   421  ```go
   422  math.Seq [start] end [step]
   423  ```
   424  
   425  ### Arguments
   426  
   427  | name | description |
   428  |------|-------------|
   429  | `start` | _(optional)_ The first number in the sequence (defaults to `1`) |
   430  | `end` | _(required)_ The last number in the sequence |
   431  | `step` | _(optional)_ The amount to increment between each number (defaults to `1`) |
   432  
   433  ### Examples
   434  
   435  ```console
   436  $ gomplate -i '{{ range (math.Seq 5) }}{{.}} {{end}}'
   437  1 2 3 4 5
   438  ```
   439  ```console
   440  $ gomplate -i '{{ conv.Join (math.Seq 10 -3 2) ", " }}'
   441  10, 8, 6, 4, 2, 0, -2
   442  ```
   443  
   444  ## `math.Sub`
   445  
   446  **Alias:** `sub`
   447  
   448  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`.
   449  
   450  ### Usage
   451  
   452  ```go
   453  math.Sub a b
   454  ```
   455  ```go
   456  b | math.Sub a
   457  ```
   458  
   459  ### Arguments
   460  
   461  | name | description |
   462  |------|-------------|
   463  | `a` | _(required)_ The minuend (the number to subtract from) |
   464  | `b` | _(required)_ The subtrahend (the number being subtracted) |
   465  
   466  ### Examples
   467  
   468  ```console
   469  $ gomplate -i '{{ math.Sub 3 1 }}'
   470  2
   471  ```