Skip to content

Fix non-transitive comparison of large integers and floats - #343

Closed
MaxWang007 wants to merge 1 commit into
itchyny:mainfrom
MaxWang007:cursor/fix-mixed-int-float-compare-ddc5
Closed

MaxWang007 wants to merge 1 commit into
itchyny:mainfrom
MaxWang007:cursor/fix-mixed-int-float-compare-ddc5

Conversation

@MaxWang007

Copy link
Copy Markdown

Fixes #342

Integer/float comparisons used to convert the integer through float64, so values past 2^53 were rounded. Equality was then non-transitive: unique could drop 2^53+1 or keep it depending on input order, and a finite 2^1100 compared equal to +Inf.

This compares the integer with the exact value of the float64 instead. Arithmetic such as addition is unchanged and still goes through float64.

$ gojq -nc '[9007199254740992.0, 9007199254740992, 9007199254740993] | unique'
[9007199254740992,9007199254740993]

$ gojq -nc '[9007199254740992 == 9007199254740993,
             9007199254740993 == 9007199254740992.0,
             9007199254740992 == 9007199254740992.0]'
[false,false,true]

Tests:

  • go test -race ./...
  • GOARCH=386 go test ./...
  • staticcheck -checks all,-ST1000 -tags gojq_debug ./...

Integer/float comparisons converted the integer through float64, so
2^53+1 compared equal to 2^53.0 and a finite 2^1100 compared equal to
+Inf. Compare the integer with the float's exact value instead.
@itchyny itchyny closed this Nov 24, 2026
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.

Mixed integer/float comparisons can make unique discard a distinct integer

3 participants