Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin.jq: naive abs/0 #2767

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]'
output: ['[10,1.1,1e-1]']

- 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
17 changes: 17 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,23 @@ false
1
1

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

map(abs)
[-0, 0, -10, -1.1]
[0,0,10,1.1]

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

# The following is NOT prescriptive:
map(abs)
[0.1,1000000000000000002]
[1e-1, 1000000000000000002]

# 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.