jaq icon indicating copy to clipboard operation
jaq copied to clipboard

README.md update re jq's division-by-0 behavior

Open pkoppstein opened this issue 1 year ago • 2 comments

The paragraph about division by 0 is outdated. Note also that jq's current (1.6) behavior is dependent on whether the numerator is a literal 0 or a variable equal to 0.

$ jq -n '0 as $n | $n / 0'
jq: error (at <unknown>): number (0) and number (0) cannot be divided because the divisor is zero

$ jq -n '1 as $n | $n / 0'
jq: error (at <unknown>): number (1) and number (0) cannot be divided because the divisor is zero

$ jq -n '0 / 0'
null

$ jq -n '1 / 0'
jq: error: Division by zero? at <top-level>, line 1:
1 / 0
jq: 1 compile error

pkoppstein avatar Aug 28 '22 04:08 pkoppstein

When saying "the paragraph about division by 0", are you talking about the following?

In jaq, n / 0 yields nan if n == 0, infinite if n > 0, and -infinite if n < 0.

I do not see how it is outdated, because I just tried this:

$ jaq -n '0 / 0 | isnan'
true
$ jaq -n '1 / 0 | isinfinite'
true
$ jaq -n '(-1) / 0 | isinfinite'
true

Note also that jq's current (1.6) behavior is dependent on whether the numerator is a literal 0 or a variable equal to 0.

Oh god. Do you have any idea how to describe this behaviour in the README in a concise way?

01mf02 avatar Aug 31 '22 15:08 01mf02

I was referring to the sentence:

"In jq, n / 0 yields nan (not a number) if n == 0 and fails otherwise"

Perhaps it would suffice to write:

Using jq, the behavior of expressions involving division by 0 varies by version.

If you want to describe jq 1.6, you could point out:

In jq 1.6, '(0/0) | isnan' yields true, but other expressions involving division by 0 yield errors.

pkoppstein avatar Sep 02 '22 02:09 pkoppstein