Skip to content
Myr

builtin.jq: naive abs/0

Before adoption: this commit came in with the history the project adopted, so no landing records it.

manual.yml explains that the def is naive, and mentions fabs, etc.

authored by pkoppstein · committed by Nico Williams · parent 3a1ba0c · Browse files · 5 files+53 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+53 added−0 removed

  • tests:+16 added−0 removed
  • other:+37 added−0 removed

5 files+53 added−0 removed

modified:docs/content/manual/manual.yml+15 added
848 unmodified lines
849 - program: '.[] | (1 / .)?'850 input: '[1,0,-1]'851 output: ['1', '-1']852853 - title: "`abs`"854 body: |855856 The builtin function `abs` is defined naively as: `if . < 0 then - . else . end`.857858 For numeric input, this is the absolute value. See the859 section on the identity filter for the implications of this860 definition for numeric input.861862 To compute the absolute value of a number as a floating point number, you may wish use `fabs`.863864 examples:865 - program: 'map(abs)'866 input: '[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]'867 output: ['[10,1.1,1e-1,10000000000000002,1e+18]']868869 - title: "`length`"870 body: |871872 The builtin function `length` gets the length of various
2767 unmodified lines
modified:src/builtin.jq+1 added
8 unmodified lines
9def max_by(f): _max_by_impl(map([f]));10def min_by(f): _min_by_impl(map([f]));11def add: reduce .[] as $x (null; . + $x);12def del(f): delpaths([path(f)]);13def abs: if . < 0 then - . else . end;14def _assign(paths; $value): reduce path(paths) as $p (.; setpath($p; $value));15def _modify(paths; update):16 reduce path(paths) as $p ([., []];17 . as $dot
260 unmodified lines
modified:tests/jq.test+12 added
1806 unmodified lines
1807. |= try . catch .180811809118101811# abs, fabs, length1812abs1813"abc"1814"abc"18151816map(abs)1817[-10, -1.1, -1e-1, 1000000000000000002]1818[10,1.1,0.1,1000000000000000002]18191820map(fabs == length) | unique[0]1821[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]1822true18231824# Using a keyword as variable/label name18251826123 as $label | $label1827null
112 unmodified lines
modified:tests/man.test+4 added
186 unmodified lines
187[1,0,-1]1881189-1190191map(abs)192[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]193[10,1.1,1e-1,10000000000000002,1e+18]194195.[] | length196[[1,2], "string", {"a":2}, null, -5]19721986
749 unmodified lines
modified:jq.1.prebuilt+21 added
833 unmodified lines
834.fi835.836.IP "" 0837.838.SS "abs"839The builtin function \fBabs\fR is defined naively as: \fBif \. < 0 then \- \. else \. end\fR\.840.841.P842For numeric input, this is the absolute value\. See the section on the identity filter for the implications of this definition for numeric input\.843.844.P845To compute the absolute value of a number as a floating point number, you may wish use \fBfabs\fR\.846.847.IP "" 4848.849.nf850851jq \'map(abs)\'852 [\-10, \-1\.1, \-1e\-1, 1000000000000000002, \-1000000000000000002]853=> [10,1\.1,1e\-1,10000000000000002,1e+18]854.855.fi856.857.IP "" 0858.859.SS "length"860The builtin function \fBlength\fR gets the length of various different types of value:861.862.IP "\(bu" 4
3228 unmodified lines