Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #10107
Closes #10115
The main cause of the old problems is that
LibGMP.get_ui
andget_si
return basically garbage if the number would have overflowed. But the code assumed that it truncates the number correctly, same as Crystal.https://gmplib.org/manual/Converting-Integers
mpz_get_ui
mpz_get_si
So this takes matters into our own hands and pre-truncates the number with modulo where appropriate.
Another issue was that for non-
!
variants it was checking only for negative arguments, but all other cases silently "garbage-overflowed".This only fixes the *-64 variants, the others are still broken, though hopefully not as horribly.
The hash itself obviously can just work with
to_i64!
and actually there's no better implementation than that, so I just fixed it.I'm actually having some difficulty in making the hashes equal with both
Int64
< 0 andUInt64
>Int64::MAX
at the same time, so I chose to skip the latter (so, for numbers greater thanInt64::MAX
hashes acrossUInt64
andBigInt
don't actually match). Too difficult to fix everything for now.It's curious, it seems like the code at the bottom of the file big_int.cr tries to fix this, but the code is unused and became broken!