From 62829dc9d34a84f892a0b5c5829faebf89c787f3 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Mon, 17 Jun 2019 12:34:30 -0500 Subject: [PATCH] Add untrunc operator, the mirror of trunc (#3994) --- core/modules/filters/math.js | 4 ++++ editions/prerelease/tiddlers/Release 5.1.20.tid | 2 +- .../examples/untrunc Operator (Examples).tid | 9 +++++++++ .../tw5.com/tiddlers/filters/trunc Operator.tid | 2 ++ .../tw5.com/tiddlers/filters/untrunc Operator.tid | 15 +++++++++++++++ 5 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 editions/tw5.com/tiddlers/filters/examples/untrunc Operator (Examples).tid create mode 100644 editions/tw5.com/tiddlers/filters/untrunc Operator.tid diff --git a/core/modules/filters/math.js b/core/modules/filters/math.js index 801ebf5ed..c3f6d3b45 100644 --- a/core/modules/filters/math.js +++ b/core/modules/filters/math.js @@ -43,6 +43,10 @@ exports.trunc = makeNumericBinaryOperator( function(a) {return Math.trunc(a)} ); +exports.untrunc = makeNumericBinaryOperator( + function(a) {return Math.ceil(Math.abs(a)) * Math.sign(a)} +); + exports.sign = makeNumericBinaryOperator( function(a) {return Math.sign(a)} ); diff --git a/editions/prerelease/tiddlers/Release 5.1.20.tid b/editions/prerelease/tiddlers/Release 5.1.20.tid index 47baa720a..aac0e9259 100644 --- a/editions/prerelease/tiddlers/Release 5.1.20.tid +++ b/editions/prerelease/tiddlers/Release 5.1.20.tid @@ -9,7 +9,7 @@ type: text/vnd.tiddlywiki !! Major Improvements -Added several new [[Mathematics Operators]] for working with numbers: [[negate|negate Operator]], [[abs|abs Operator]], [[ceil|ceil Operator]], [[floor|floor Operator]], [[round|round Operator]], [[trunc|trunc Operator]], [[sign|sign Operator]], [[add|add Operator]], [[subtract|subtract Operator]], [[multiply|multiply Operator]], [[divide|divide Operator]], [[remainder|remainder Operator]], [[max|max Operator]], [[min|min Operator]], [[fixed|fixed Operator]], [[precision|precision Operator]], [[exponential|exponential Operator]], [[sum|sum Operator]], [[product|product Operator]], [[maxall|maxall Operator]] and [[minall|minall Operator]]. +Added several new [[Mathematics Operators]] for working with numbers: [[negate|negate Operator]], [[abs|abs Operator]], [[ceil|ceil Operator]], [[floor|floor Operator]], [[round|round Operator]], [[trunc|trunc Operator]], [[untrunc|untrunc Operator]], [[sign|sign Operator]], [[add|add Operator]], [[subtract|subtract Operator]], [[multiply|multiply Operator]], [[divide|divide Operator]], [[remainder|remainder Operator]], [[max|max Operator]], [[min|min Operator]], [[fixed|fixed Operator]], [[precision|precision Operator]], [[exponential|exponential Operator]], [[sum|sum Operator]], [[product|product Operator]], [[maxall|maxall Operator]] and [[minall|minall Operator]]. Added several new string operators: [[length|length Operator]], [[uppercase|uppercase Operator]], [[lowercase|lowercase Operator]], [[trim|trim Operator]], [[split|split Operator]], [[splitregexp|split Operator]] and [[join|join Operator]]. diff --git a/editions/tw5.com/tiddlers/filters/examples/untrunc Operator (Examples).tid b/editions/tw5.com/tiddlers/filters/examples/untrunc Operator (Examples).tid new file mode 100644 index 000000000..a1415001a --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/examples/untrunc Operator (Examples).tid @@ -0,0 +1,9 @@ +created: 20190617162921280 +modified: 20190617162921280 +tags: [[untrunc Operator]] [[Operator Examples]] +title: untrunc Operator (Examples) +type: text/vnd.tiddlywiki + +<<.operator-example 1 "[[1.6]untrunc[]]">> +<<.operator-example 2 "[[-1.6]untrunc[]]">> +<<.operator-example 3 "=-1.2 =-2.4 =3.6 =4.8 =5.1 +[untrunc[]]">> diff --git a/editions/tw5.com/tiddlers/filters/trunc Operator.tid b/editions/tw5.com/tiddlers/filters/trunc Operator.tid index 43cf2d2dd..72bb23534 100644 --- a/editions/tw5.com/tiddlers/filters/trunc Operator.tid +++ b/editions/tw5.com/tiddlers/filters/trunc Operator.tid @@ -10,4 +10,6 @@ type: text/vnd.tiddlywiki <<.from-version "5.1.20">> See [[Mathematics Operators]] for an overview. +<<.tip " The trunc operator rounds towards zero. To round away from zero, use [[untrunc|untrunc Operator]].">> + <<.operator-examples "trunc">> diff --git a/editions/tw5.com/tiddlers/filters/untrunc Operator.tid b/editions/tw5.com/tiddlers/filters/untrunc Operator.tid new file mode 100644 index 000000000..9aaf06295 --- /dev/null +++ b/editions/tw5.com/tiddlers/filters/untrunc Operator.tid @@ -0,0 +1,15 @@ +caption: untrunc +created: 20190617163327756 +modified: 20190617163327756 +op-input: a [[selection of titles|Title Selection]] +op-output: rounds each of the input numbers to the next integer with largest absolute value, that is, away from zero +op-purpose: rounds a list of numbers to the next integer with largest absolute value, that is, away from zero +tags: [[Unary Mathematics Operators]] [[Filter Operators]] [[Mathematics Operators]] +title: untrunc Operator +type: text/vnd.tiddlywiki + +<<.from-version "5.1.20">> See [[Mathematics Operators]] for an overview. + +<<.tip " This is the inverse operation of [[trunc|trunc Operator]]: trunc rounds to zero, but untrunc rounds away from zero.">> + +<<.operator-examples "untrunc">>