2019-02-07 11:19:06 +00:00
created: 20190206140446821
2021-04-27 09:09:13 +00:00
modified: 20210417090408263
2019-02-07 11:19:06 +00:00
tags: Filters
2019-06-11 16:18:51 +00:00
title: Mathematics Operators
type: text/vnd.tiddlywiki
2019-02-07 11:19:06 +00:00
<<.from-version "5.1.20">>The mathematics filter operators allow numerical calculations to be performed within filters.
The mathematics operators interpret their arguments as numbers according to the following rules:
* Any non-numeric characters after a valid number are ignored (e.g. `28px` is interpreted as the number 28)
* If the argument cannot be interpreted as a number, the value 0 is used (e.g. `foo` is interpreted as the number 0)
* The special values `Infinity` and `-Infinity` can be used to represent positive and negative infinity respectively
2019-06-11 16:18:51 +00:00
<$macrocall $name=".warning" _="Some filter operators remove duplicate items which can cause unexpected results when using the mathematics operators. See [[Dominant Append]] for details."/>
2019-02-07 11:19:06 +00:00
The mathematics operators take three different forms:
* ''Unary operators'' apply an operation to each number in the input list (e.g. negate, truncate, sign)
2019-06-17 20:21:23 +00:00
** <<.inline-operator-example "=1 =2 =3 =4 +[negate[]]">>
** <<.inline-operator-example "=1.2 =2.4 =3.6 =4.8 +[trunc[]]">>
** <<.inline-operator-example "=1.2 =2.4 =3.6 =4.8 +[round[]]">>
2019-02-07 11:19:06 +00:00
* ''Binary operators'' apply an operation and operand to each number in the input list (e.g. add, multiply, remainder)
2019-06-17 20:21:23 +00:00
** <<.inline-operator-example "=1 =2 =3 =4 +[add[3]]">>
** <<.inline-operator-example "=1 =2 =3 =4 +[multiply[8]]">>
2019-06-11 16:18:51 +00:00
* ''Reducing operators'' apply an operation to all of the numbers in the input list, returning a single result (e.g. sum, product)
2019-06-17 20:21:23 +00:00
** <<.inline-operator-example "=1 =2 =3 =4 +[sum[]]">>
** <<.inline-operator-example "=1 =2 =3 =4 +[product[]]">>
2021-04-27 09:09:13 +00:00
** <<.inline-operator-example "=1 =2 =3 =4 +[average[]]">>
2019-02-07 11:19:06 +00:00
Operators can be combined:
* <<.inline-operator-example "[[355]divide[113]fixed[5]]">>
* <<.inline-operator-example "[range[100]sum[]divide[100]]">>
* <<.inline-operator-example "[tag[HelloThere]get[text]length[]sum[]]">>
Complex operations will sometimes need to be split up into separate filters. For example, to compute the average length of the text field of tiddlers tagged "~HelloThere":
<$macrocall $name='wikitext-example-without-html'
src="""<$set name="number-of-tiddlers" value={{{ [tag[HelloThere]count[]] }}}>
Average length of <$text text=<<number-of-tiddlers>>/> tiddlers tagged <<tag "HelloThere">>: <$text text={{{ [tag[HelloThere]get[text]length[]sum[]divide<number-of-tiddlers>fixed[3]] }}}/>
</$set>""" />
<<list-links "[tag[Mathematics Operators]]">>