Skip to content

Commit

Permalink
Make 0 divided by 0 results in NaN consistently (#2253)
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Jul 4, 2023
1 parent dfd440f commit 886a9b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ static jv f_multiply(jq_state *jq, jv input, jv a, jv b) {
static jv f_divide(jq_state *jq, jv input, jv a, jv b) {
jv_free(input);
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
if (jv_number_value(b) == 0.0)
if (jv_number_value(b) == 0.0 && jv_number_value(a) != 0.0)
return type_error2(a, b, "cannot be divided because the divisor is zero");
jv r = jv_number(jv_number_value(a) / jv_number_value(b));
jv_free(a);
Expand Down
5 changes: 5 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,11 @@ try (1/.) catch .
0
"number (1) and number (0) cannot be divided because the divisor is zero"

0/0, (0 as $x | $x/0) | isnan
0
true
true

try (1%.) catch .
0
"number (1) and number (0) cannot be divided (remainder) because the divisor is zero"
Expand Down

0 comments on commit 886a9b1

Please sign in to comment.