Project

General

Profile

Actions

Feature #20811

closed

`warning: in a**b, b may be too big` is really helpful?

Added by mame (Yusuke Endoh) 29 days ago. Updated 14 days ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:119602]

Description

I tried to calculate the largest prime number recently discovered. However, it did not work. I was a bit disappointed.

$ ruby -e 'p 2**136279841-1'
-e:1: warning: in a**b, b may be too big
Infinity

I know this is not a realistic case, but I checked my past chat logs and found a record of the same thing six years ago and the same disappointment.

$ ruby -e 'p 2**77232917-1'
-e:1: warning: in a**b, b may be too big
Infinity

So I would like to ask, has anyone experienced that this behavior has actually been helpful?

I think that this limit is to prevent a program from unintentionally spending too much time and memory trying to compute a power that is too large. However, is it helpful to return Infinity in such a situation? I think raising an exception would be safer, if this limit is really needed.

Also, is this limit appropriate? On my machine, 2 ** 32537661 is calculated normally and 2 ** 32537662 returns Infinity. This calculation took about 100 ms and 400 kB. I think this is short enough for a single computation time. Incidentally, 2 ** 32537661 in decimal notation is 9,794,814 digits, which may be too loose a limit considering the display.

Note that this limit only applies when calculating the power directly. (2 ** 32537661) * 2 returns an Integer, not Infinity. Even 1 << 32537662 gives an Integer.

From the above, I suspect that this limit is not particularly useful and only spoils the fun of mathematics. What do you think?

Updated by byroot (Jean Boussier) 29 days ago

Agreed. Either Ruby can't do it and should raise some sort of error, or just do it even if it uses gigabytes of memory.

That warning isn't helping anyone, and returning Infinity is a huge gotcha and totally unexpected.

Updated by Eregon (Benoit Daloze) 29 days ago

Agreed it should just compute it.
It should still be interruptible by Ctrl+C though, but it probably already is.

Updated by mame (Yusuke Endoh) 15 days ago

Matz said "let's calculate". I will create a PR

Updated by mame (Yusuke Endoh) 14 days ago

I have created https://github.com/ruby/ruby/pull/12033

This patch changes the behavior of Integer#** and Rational#**. If the generated bignum is not likely to exceed an estimated 16 GB, it will be calculated straightforwardly. If it is likely to exceed 16 GB, an ArgumentError is raised.

Complex#** also prints a warning "in a**b, b may be too big", but I found a bit more subtle issue, so I will create a separate ticket.

Actions #5

Updated by mame (Yusuke Endoh) 14 days ago

  • Status changed from Open to Closed

Applied in changeset git|45cd4a8296814f3b082dfb906cdef29974726731.


Do not round a**b to infinity

... instead, just calculate the value unless it is too big.
Also, this change raises an ArgumentError if it is expected to exceed
16 GB in a 64-bit environment.

(It is possible to calculate it straightforward, but it would likely be
out-of-memory, so I didn't think it would make sense.)

[Feature #20811]

Actions

Also available in: Atom PDF

Like2
Like0Like0Like0Like0Like0