Skip to content

Commit

Permalink
builtin.jq: naive abs/0
Browse files Browse the repository at this point in the history
manual.yml explains that the def is naive, and mentions fabs, etc.
  • Loading branch information
pkoppstein committed Jul 25, 2023
1 parent 8f49600 commit ebd4775
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,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: |
Expand Down
21 changes: 21 additions & 0 deletions jq.1.prebuilt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([., []];
Expand Down
12 changes: 12 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,18 @@ false
1
1

# abs, fabs, length
abs
"abc"
"abc"

map(abs)
[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]
[10,1.1,0.1,1000000000000000002,1e+18]

map(fabs == length) | unique[0]
[-10, -1.1, -1e-1, 1000000000000000002, -1000000000000000002]
true

# Using a keyword as variable/label name

Expand Down
4 changes: 4 additions & 0 deletions tests/man.test

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebd4775

Please sign in to comment.