diff options
author | pkoppstein <pkoppstein@gmail.com> | 2023-07-24 23:25:42 -0400 |
---|---|---|
committer | Nico Williams <nico@cryptonector.com> | 2023-07-25 23:43:57 -0500 |
commit | 7b725378b9803a1df74054ebd3d700d9daeb2049 (patch) | |
tree | fd279b6868d12ca5b79e97989eb8439d1d7608cb | |
parent | 3a1ba0c42d2deb80b3f94b7993c7096cfe33b3bb (diff) |
builtin.jq: naive abs/0
manual.yml explains that the def is naive, and mentions fabs, etc.
-rw-r--r-- | docs/content/manual/manual.yml | 15 | ||||
-rw-r--r-- | jq.1.prebuilt | 21 | ||||
-rw-r--r-- | src/builtin.jq | 1 | ||||
-rw-r--r-- | tests/jq.test | 12 | ||||
-rw-r--r-- | tests/man.test | 4 |
5 files changed, 53 insertions, 0 deletions
diff --git a/docs/content/manual/manual.yml b/docs/content/manual/manual.yml index ae9a8498..7f28150d 100644 --- a/docs/content/manual/manual.yml +++ b/docs/content/manual/manual.yml @@ -850,6 +850,21 @@ sections: input: '[1,0,-1]' output: ['1', '-1'] + - title: "`abs`" + body: | + + The builtin function `abs` is defined naively as: `if . < 0 then - . else . end`. + + For numeric input, this is the absolute value. See the + section on the identity filter for the implications of this + definition for numeric input. + + To compute the absolute value of a number as a floating point number, you may wish use `fabs`. + + examples: + - program: 'map(abs)' + input: '[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]' + output: ['[10,1.1,1e-1,10000000000000002,1e+18]'] - title: "`length`" body: | diff --git a/jq.1.prebuilt b/jq.1.prebuilt index c9042ba2..ed7dc0d7 100644 --- a/jq.1.prebuilt +++ b/jq.1.prebuilt @@ -835,6 +835,27 @@ jq \'\.[] | (1 / \.)?\' . .IP "" 0 . +.SS "abs" +The builtin function \fBabs\fR is defined naively as: \fBif \. < 0 then \- \. else \. end\fR\. +. +.P +For numeric input, this is the absolute value\. See the section on the identity filter for the implications of this definition for numeric input\. +. +.P +To compute the absolute value of a number as a floating point number, you may wish use \fBfabs\fR\. +. +.IP "" 4 +. +.nf + +jq \'map(abs)\' + [\-10, \-1\.1, \-1e\-1, 1000000000000000002, \-1000000000000000002] +=> [10,1\.1,1e\-1,10000000000000002,1e+18] +. +.fi +. +.IP "" 0 +. .SS "length" The builtin function \fBlength\fR gets the length of various different types of value: . diff --git a/src/builtin.jq b/src/builtin.jq index 45ba3060..e74e9771 100644 --- a/src/builtin.jq +++ b/src/builtin.jq @@ -10,6 +10,7 @@ def max_by(f): _max_by_impl(map([f])); def min_by(f): _min_by_impl(map([f])); def add: reduce .[] as $x (null; . + $x); def del(f): delpaths([path(f)]); +def abs: if . < 0 then - . else . end; def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value)); def _modify(paths; update): reduce path(paths) as $p ([., []]; diff --git a/tests/jq.test b/tests/jq.test index 95b51366..e79d60ca 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -1808,6 +1808,18 @@ false 1 1 +# abs, fabs, length +abs +"abc" +"abc" + +map(abs) +[-10, -1.1, -1e-1, 1000000000000000002] +[10,1.1,0.1,1000000000000000002] + +map(fabs == length) | unique[0] +[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002] +true # Using a keyword as variable/label name diff --git a/tests/man.test b/tests/man.test index cce2edb3..682c24aa 100644 --- a/tests/man.test +++ b/tests/man.test @@ -188,6 +188,10 @@ null 1 -1 +map(abs) +[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002] +[10,1.1,1e-1,10000000000000002,1e+18] + .[] | length [[1,2], "string", {"a":2}, null, -5] 2 |