builtin.jq: naive abs/0

manual.yml explains that the def is naive, and mentions fabs, etc.
This commit is contained in:
pkoppstein authored and Nico Williams committed 2023-07-25 23:43:57 -05:00
1 parent 3a1ba0c42d
commit 7b725378b9
5 files changed
+53

No files matched your search

+15
View File
@@ -850,6 +850,21 @@ sections:
input: '[1,0,-1]' input: '[1,0,-1]'
output: ['1', '-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`" - title: "`length`"
body: | body: |
Generated
+21
View File
@@ -835,6 +835,27 @@ jq \'\.[] | (1 / \.)?\'
. .
.IP "" 0 .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" .SS "length"
The builtin function \fBlength\fR gets the length of various different types of value: The builtin function \fBlength\fR gets the length of various different types of value:
. .
+1
View File
@@ -10,6 +10,7 @@ def max_by(f): _max_by_impl(map([f]));
def min_by(f): _min_by_impl(map([f])); def min_by(f): _min_by_impl(map([f]));
def add: reduce .[] as $x (null; . + $x); def add: reduce .[] as $x (null; . + $x);
def del(f): delpaths([path(f)]); 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 _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));
def _modify(paths; update): def _modify(paths; update):
reduce path(paths) as $p ([., []]; reduce path(paths) as $p ([., []];
+12
View File
@@ -1808,6 +1808,18 @@ false
1 1
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 # Using a keyword as variable/label name
+4
View File
@@ -188,6 +188,10 @@ null
1 1
-1 -1
map(abs)
[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]
[10,1.1,1e-1,10000000000000002,1e+18]
.[] | length .[] | length
[[1,2], "string", {"a":2}, null, -5] [[1,2], "string", {"a":2}, null, -5]
2 2