Skip to content

docs: correct the floor division example - #341

Open
akzarma wants to merge 1 commit into
itchyny:mainfrom
akzarma:docs/gojq-precise-floor-division
Open

akzarma wants to merge 1 commit into
itchyny:mainfrom
akzarma:docs/gojq-precise-floor-division

Conversation

@akzarma

@akzarma akzarma commented Nov 22, 2026

Copy link
Copy Markdown

The README calls the idivide example floor division, but subtracting the remainder only truncates the quotient toward zero. The original example returns -2 for -5 / 2; floor division should return -3.

Subtract one more when the nonzero remainder and divisor have opposite signs. The division itself remains exact, so the example still works for integers larger than a float can represent. This is a README-only change.

Validation on Go 1.26.0 (macOS arm64): the exact documented query matched integer floor division across 882 cases, including both divisor signs, zero numerators, exact divisibility, and 127-bit values. go test -mod=readonly -p 2 -count=1 ./... and git diff --check passed.

AI assistance was used to prepare and verify this documentation change.

Comment thread README.md
- gojq is purely implemented with Go language and is completely portable. jq depends on the C standard library so the availability of math functions depends on the library. jq also depends on the regular expression library and it makes build scripts complex.
- gojq does not keep the order of object keys. I understand this might cause problems for some scripts but basically, we should not rely on the order of object keys. Due to this limitation, gojq does not have `keys_unsorted` function and `--sort-keys` (`-S`) option. I would implement when ordered map is implemented in the standard library of Go but I'm less motivated.
- gojq supports arbitrary-precision integer calculation while jq does not; jq loses the precision of large integers when calculation is involved. Note that even with gojq, all mathematical functions, including `floor` and `round`, convert integers to floating-point numbers; only addition, subtraction, multiplication, modulo, and division operators (when divisible) keep the integer precision. To calculate floor division of integers without losing the precision, use `def idivide($n): (. - . % $n) / $n;`. To round down floating-point numbers to integers, use `def ifloor: floor | tostring | tonumber;`, but note that this function does not work with large floating-point numbers and also loses the precision of large integers.
- gojq supports arbitrary-precision integer calculation while jq does not; jq loses the precision of large integers when calculation is involved. Note that even with gojq, all mathematical functions, including `floor` and `round`, convert integers to floating-point numbers; only addition, subtraction, multiplication, modulo, and division operators (when divisible) keep the integer precision. To calculate floor division of integers without losing precision, use `def idivide($n): (. % $n) as $r | (. - $r) / $n - (if $r * $n < 0 then 1 else 0 end);` (for example, `-5 | idivide(2)` returns `-3`). To round down floating-point numbers to integers, use `def ifloor: floor | tostring | tonumber;`, but note that this function does not work with large floating-point numbers and also loses the precision of large integers.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like sudden for example sentence here. Also, def idivide($n): (. - (. % $n + $n) % $n) / $n; is a simpler definition I'd like to take. Honestly I'd like to leave users to define a function like this (I'd like rather dropping), but it's okay fixing for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants