-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inexplicable arithmetic overflow ?? #15341
Comments
Thanks for this bug report. Unfortunately, it's not clear how to reproduce this issue. Also it would help if you could try to reduce the code (i.e. throw away stuff that isn't necessary) and add some instrumentation in to figure out where exactly the exception is thrown. Enabling |
It's not super clear, but you can reproduce this by: crystal build --release --mcpu native prime_pairs.cr
./prime_pairs 148_000_006 Which results in the error within a few minutes. I ran it with a locally built compiler, which resulted in this trace: Unhandled exception: Arithmetic overflow (OverflowError)
from /Users/george/crystal/src/hash.cr:833:10 in 'indices_malloc_size'
from /Users/george/crystal/src/hash.cr:828:5 in 'malloc_indices'
from /Users/george/crystal/src/hash.cr:252:9 in 'initialize:initial_capacity'
from /Users/george/crystal/src/hash.cr:225:3 in 'new:initial_capacity'
from /Users/george/crystal/src/set.cr:37:5 in 'initialize'
from /Users/george/crystal/src/set.cr:36:3 in 'new'
from /Users/george/crystal/src/set.cr:45:5 in 'new'
from /Users/george/crystal/src/set.cr:509:5 in 'to_set'
from /Users/george/crystal/src/array.cr:1908:5 in 'uniq'
from prime_pairs.cr:35:22 in 'prime_pairs'
from prime_pairs.cr:40:30 in 'gen_pcp'
from prime_pairs.cr:49:1 in '__crystal_main'
from /Users/george/crystal/src/crystal/main.cr:118:5 in 'main_user_code'
from /Users/george/crystal/src/crystal/main.cr:104:7 in 'main'
from /Users/george/crystal/src/crystal/system/unix/main.cr:9:3 in 'main' So seems to actually be a limitation of |
Here is a shortened version that runs just the problem section
Sample output.
|
So looks like |
Yes, it wasn't an arithmetic overflow, which makes me feel sane again. I ran Here's truncated test data for it to show it completed without a problem.
|
FYI I'm able to run the Ruby version for n = 148_000_006 for Ruby 3.4.1|3.3.6, JRuby 9.4.9.0 (Truffleruby 24.1.1 gives out of mem ).
So 16GB was enough for them to run this in. Here's the almost identical Ruby code.
I would think if there's enough memory for Ruby to run and completer then so should Crystal. |
The exception raises on the multiplication This works fine in Ruby because Ruby's |
More specifically: you can only address as many as You can allocate more than Int32::MAX bytes in a collection but the maximum contiguous memory will always be To overcome this limitation, you need specific types (or wrapper types). |
Hm, actually it seems we can enable this by performing the multiplication in @@ -831,5 +831,5 @@ class Hash(K, V)
# The actual number of bytes needed to allocate `@indices`.
private def indices_malloc_size(size)
- size * @indices_bytesize
+ size.to_u32 * @indices_bytesize
end This works because |
Oh right. I guess we could even go for |
This issue has been mentioned on Crystal Forum. There might be relevant details there: https://forum.crystal-lang.org/t/goldbachs-conjecture-and-crystal-code/7579/18 |
fixes crystal-lang#15341 Co-authored-by: Johannes Müller <[email protected]>
I'm encountering a seemingly random arithmetic overflow error message in code that shouldn't overflow.
Below is the code.
I compile it like this.
Run it like this,
The overflow message occurs in this part.
At no time does the math create values > n-2.
Here is truncated output with the print statement for n = 150_000_006 that doesn't overflow.
This executes correctly for n = 150_000_006, when ri > ri_max at the end.
You see, ri_max is continually getting smaller.
This overflows for a smaller value of n = 148_000_006, when ri > ri_max at the end.
All I can think of is
>
is somehow the culprit?For some reason, it bombs for n= 148_000_006 when ri = 12169 > ri_max = 12168
Both these values are way, way less than 148_000_006, so how can there be an overflow?
And it occurs with apparently random values of different sizes (larger|smaller).
The problem occurs using Crystal versions 1.15.0, 1.14.0, 1.13.2 and 1.12.2.
The Ruby version, which is almost code identical, works for all values with no problems.
The text was updated successfully, but these errors were encountered: