Project

General

Profile

Actions

Feature #20811

closed

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

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

Added by mame (Yusuke Endoh) about 1 year ago. Updated 12 months 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?


Related issues 1 (0 open1 closed)

Related to Ruby - Feature #21650: Performance regression: Rational#floor(ndigits) extremely slow for huge ndigits in Ruby 3.4 (ok in 3.2)FeedbackActions

Updated by byroot (Jean Boussier) about 1 year ago Actions #1 [ruby-core:119603]

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) about 1 year ago Actions #2 [ruby-core:119604]

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) 12 months ago Actions #3 [ruby-core:119798]

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

Updated by mame (Yusuke Endoh) 12 months ago Actions #4 [ruby-core:119838]

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.

Updated by mame (Yusuke Endoh) 12 months ago Actions #5

  • 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]

Updated by mame (Yusuke Endoh) 1 day ago Actions #6

  • Related to Feature #21650: Performance regression: Rational#floor(ndigits) extremely slow for huge ndigits in Ruby 3.4 (ok in 3.2) added
Actions

Also available in: PDF Atom