Skip to content

Commit

Permalink
Fold modulo operator on constant values and raise zero remainder erro…
Browse files Browse the repository at this point in the history
…r quickly
  • Loading branch information
itchyny authored and nicowilliams committed Jul 24, 2023
1 parent 3305596 commit a60140b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser.c

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

1 change: 1 addition & 0 deletions src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ static block constant_fold(block a, block b, int op) {
case '-': res = jv_number(na - nb); break;
case '*': res = jv_number(na * nb); break;
case '/': res = jv_number(na / nb); break;
case '%': res = jv_number((intmax_t)nb == 0 ? INFINITY : (intmax_t)na % (intmax_t)nb); break;
case EQ: res = (cmp == 0 ? jv_true() : jv_false()); break;
case NEQ: res = (cmp != 0 ? jv_true() : jv_false()); break;
case '<': res = (cmp < 0 ? jv_true() : jv_false()); break;
Expand Down
4 changes: 4 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,10 @@ try (1%.) catch .
1/0
jq: error: Division by zero? at <top-level>, line 1:

%%FAIL
1%0
jq: error: Remainder by zero? at <top-level>, line 1:

# Basic numbers tests: integers, powers of two
[range(-52;52;1)] as $powers | [$powers[]|pow(2;.)|log2|round] == $powers
null
Expand Down

0 comments on commit a60140b

Please sign in to comment.