From 89541edcff6672a70020c67cdbe5a38c2ef125d7 Mon Sep 17 00:00:00 2001 From: saqimtiaz Date: Fri, 20 Nov 2020 15:00:20 +0100 Subject: [PATCH] Added mathematics filter operators power and log (#5080) --- core/modules/filters/math.js | 14 ++++++++++++++ .../filters/examples/log Operator (Examples).tid | 14 ++++++++++++++ .../examples/power Operator (Examples).tid | 8 ++++++++ .../tw5.com/tiddlers/filters/log Operator.tid | 15 +++++++++++++++ .../tw5.com/tiddlers/filters/power Operator.tid | 15 +++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 editions/tw5.com/tiddlers/filters/examples/log Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/examples/power Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/log Operator.tid create mode 100644 editions/tw5.com/tiddlers/filters/power Operator.tid diff --git a/core/modules/filters/math.js b/core/modules/filters/math.js index ac2b40117..f52a8c678 100644 --- a/core/modules/filters/math.js +++ b/core/modules/filters/math.js @@ -91,6 +91,20 @@ exports.exponential = makeNumericBinaryOperator( function(a,b) {return Number.prototype.toExponential.call(a,Math.min(Math.max(b,0),100));} ); +exports.power = makeNumericBinaryOperator( + function(a,b) {return Math.pow(a,b);} +); + +exports.log = makeNumericBinaryOperator( + function(a,b) { + if(b) { + return Math.log(a)/Math.log(b); + } else { + return Math.log(a); + } + } +); + exports.sum = makeNumericReducingOperator( function(accumulator,value) {return accumulator + value}, 0 // Initial value diff --git a/editions/tw5.com/tiddlers/filters/examples/log Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/log Operator (Examples).tid new file mode 100644 index 000000000..371952170 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/log Operator (Examples).tid @@ -0,0 +1,14 @@ +created: 20201120041503515 +modified: 20201120042024813 +tags: [[Operator Examples]] [[log Operator]] +title: log Operator (Examples) +type: text/vnd.tiddlywiki + +Logarithm of `8` with base `2`: +<<.operator-example 1 "[[8]log[2]]">> + +Logarithm of `100` with base `10`: +<<.operator-example 2 "[[100]log[10]]">> + +Natural logarithm of 10 (base `e`), equivalent to `ln(10)` in mathematics: +<<.operator-example 2 "[[10]log[]]">> diff --git a/editions/tw5.com/tiddlers/filters/examples/power Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/power Operator (Examples).tid new file mode 100644 index 000000000..e2bfdb0f5 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/power Operator (Examples).tid @@ -0,0 +1,8 @@ +created: 20201120040913073 +modified: 20201120041513726 +tags: [[Operator Examples]] [[power Operator]] +title: power Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[3]power[3]]">> +<<.operator-example 2 "9 +[power[2]]">> diff --git a/editions/tw5.com/tiddlers/filters/log Operator.tid b/editions/tw5.com/tiddlers/filters/log Operator.tid new file mode 100644 index 000000000..bfca18853 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/log Operator.tid @@ -0,0 +1,15 @@ +caption: log +created: 20201120041113537 +modified: 20201120041443518 +op-input: a [[selection of titles|Title Selection]] +op-output: the logarithm of each input title as numbers, with base <<.place N>> if specified otherwise base `e` +op-parameter: a number +op-parameter-name: N +op-purpose: treating each input title as a number, return its logarithm with base equal to the numeric value of the operand if specified, otherwise base `e` +tags: [[Filter Operators]] [[Mathematics Operators]] [[Binary Mathematics Operators]] +title: log Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.23">> + +<<.operator-examples "log">> diff --git a/editions/tw5.com/tiddlers/filters/power Operator.tid b/editions/tw5.com/tiddlers/filters/power Operator.tid new file mode 100644 index 000000000..d4afb67ce --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/power Operator.tid @@ -0,0 +1,15 @@ +caption: power +created: 20201120040720190 +modified: 20201120040852980 +op-input: a [[selection of titles|Title Selection]] +op-output: the input as numbers, but with each one raised to the power of <<.place N>> +op-parameter: a number +op-parameter-name: N +op-purpose: treating each input title as a number, raise it to the power of the numeric value of the operand +tags: [[Filter Operators]] [[Mathematics Operators]] [[Binary Mathematics Operators]] +title: power Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.23">> + +<<.operator-examples "power">>