Feature #3429

Integer#**(large) warns "in a**b, b may be too big" and return Infinity

Added by Benoit Daloze almost 5 years ago. Updated about 3 years ago.

[ruby-core:30735]
Status:Closed
Priority:Normal
Assignee:Kenta Murata

Description

=begin
Hi,

Trying to do
int*some_large_number can result in
"warning: in a
b, b may be too big"
#=> Infinity
By example,
2
*7830457

It is not good behavior, as we can make a workaround and it works perfectly:
# 7830457 = 2 * 37 * 105817 - 1
n = (((2 ** 105817) ** 37) ** 2) / 2 # => ...8739992577 which is the good number

I think it is not consistent to return Infinity when a correct answer can be given.
And Integer**Integer should always return an Integer (Infinity is a Float)

Also, while doing some tests about this I noticed that "bignum == Float::INFINITY" or "bignum.infinite?" hangs, while it should not, as it is always false.

See the script attached for some details and a (nonsense) implementation Integer#int_pow method which show it is possible to get better results.
=end

integer_pow.rb Magnifier (1.23 KB) Benoit Daloze, 06/11/2010 10:36 PM


Related issues

Related to Backport193 - Backport #6605: Rational#round causes irb to crash when given a reasonably absurd precision value Closed 06/19/2012

Associated revisions

Revision 35081
Added by Nobuyoshi Nakada about 3 years ago

  • bignum.c (rb_big_pow): estimate result bit size more precisely. [Feature #3429]

Revision 35081
Added by Nobuyoshi Nakada about 3 years ago

  • bignum.c (rb_big_pow): estimate result bit size more precisely. [Feature #3429]

History

#1 Updated by Benoit Daloze almost 5 years ago

=begin
"And Integer*Integer should always return an Integer (Infinity is a Float)"
=> I meant Integer
*(positive Integer) of course
=end

#2 Updated by Yusuke Endoh almost 5 years ago

  • Target version changed from 1.9.2 to 2.0.0

=begin
Hi,

I agree that it is not good behavior, but it is definitely intended
and traditional behavior. Not a bug.

1.9.2 should not be changed. Let's discuss towards 1.9.3.
I move to 1.9.x feature request.

--
Yusuke Endoh mame@tsg.ne.jp
=end

#3 Updated by Benoit Daloze almost 5 years ago

=begin
On 11 June 2010 15:58, Yusuke Endoh redmine@ruby-lang.org wrote:
| I agree that it is not good behavior, but it is definitely intended
| and traditional behavior.  Not a bug.

For a mathematician, and even if he takes care of computing
limitations, he would think it is a bug I believe ;)
Python, by example, does not complain (and it works) for this.
But yes, this behavior is common in many languages I suppose, but as
we have Bignum built-in, it seems logical Integer*(Integer>0) returns
an Integer, whatever the size is, at least until it becomes too bad
for the memory.

| 1.9.2 should not be changed.  Let's discuss towards 1.9.3.
| I move to 1.9.x feature request.

Sure, I was hesitating for the target. As I think it is important
because it is basic maths, I did choose 1.9.2.

| Yusuke Endoh mame@tsg.ne.jp

B.D

=end

#4 Updated by Hiroshi Nakamura about 3 years ago

  • Description updated (diff)
  • Assignee set to Kenta Murata

mrkn, please handle this.

#5 Updated by Nobuyoshi Nakada about 3 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r35081.
Benoit, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • bignum.c (rb_big_pow): estimate result bit size more precisely. [Feature #3429]

Also available in: Atom PDF