# Bignum limits?

**URL:** <https://www.ruby-forum.com/t/bignum-limits/67330>\
**Category:** Ruby\
**Created:** [September 2, 2006, 11:12am UTC](https://www.ruby-forum.com/t/bignum-limits/67330 "2006-09-02T11:12:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 11:12am UTC](https://www.ruby-forum.com/t/bignum-limits/67330/1 "2006-09-02T11:12:08Z")

</div>

Hi,

is there a limit on a Bignum size ?  
I get this:

irb\> a=2 **262144; a % 10** 20  
=\> 62605349934298300416

irb\> a=2**262145; a  
(irb):1: warning: in a**b, b may be too big  
=\> Infinity

But then,

irb(main):004:0\> b=a+a; b % 10\*\*20  
=\> 25210699868596600832

and even:

irb\> c=a **31; c % 10** 20  
=\> 54593967939561455616

The problem continues here:

irb\> d=a **32; d % 10** 20  
(irb):21: warning: in a\*\*b, b may be too big  
(irb):21: warning: Bignum out of Float range  
=\> NaN

But I can compute this value anyway:

irb\> e=c\*a; e % 10\*\*20  
=\> 85551374411818336256

Is there a bug in the \*\* method ?

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 11:30am UTC](https://www.ruby-forum.com/t/bignum-limits/67330/2 "2006-09-02T11:30:10Z")

</div>

Jean-Claude A. wrote:

> =\> Infinity  
> It’s with ruby 1.8.5. Don’t try to print the  
> result with ruby 1.8.2, because the computation works,  
> and the result is large ! 🙂

For example, in 1.8.2:

irb(main):001:0\> a=2 **100000000; a%10** 20  
=\> 4130048177787109376

(computations with 3\*\*n are much more slower,  
but it was expected, since ruby uses base 2)

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 11:48am UTC](https://www.ruby-forum.com/t/bignum-limits/67330/3 "2006-09-02T11:48:10Z")

</div>

Paul L. wrote:

> What version of Ruby do you have? I ran the same test and didn’t get this  
> message. And what platform? Mine:
> 
> $ ruby -v  
> ruby 1.8.4 (2005-12-24) [i386-linux]

% ruby -v  
ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]  
(that is, MacOS X 10.4.7)

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 11:38am UTC](https://www.ruby-forum.com/t/bignum-limits/67330/4 "2006-09-02T11:38:10Z")

</div>

Jean-Claude A. wrote:

> Hi,
> 
> is there a limit on a Bignum size ?

AFAIK no, apart from increasingly long computation times.

> I get this:
> 
> irb\> a=2 **262144; a % 10** 20  
> =\> 62605349934298300416
> 
> irb\> a=2**262145; a  
> (irb):1: warning: in a**b, b may be too big  
> =\> Infinity

What version of Ruby do you have? I ran the same test and didn’t get  
this  
message. And what platform? Mine:

$ ruby -v  
ruby 1.8.4 (2005-12-24) [i386-linux]

Using irb I computed 2\*\*262145 and compared it to an authoritative  
source,  
it is correct, no errors or warnings.

// snip examples

> Is there a bug in the \*\* method ?

On my system, using your examples, I cannot produce any of the warnings  
you  
are showing, and the results look correct.

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 11:52am UTC](https://www.ruby-forum.com/t/bignum-limits/67330/5 "2006-09-02T11:52:10Z")

</div>

Jean-Claude A. wrote:

> (that is, MacOS X 10.4.7)  
> Okay, I think you are seeing a version-specific bug. Any other reader  
> comments?

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/bignum-limits/67330/6 "2006-09-02T14:22:22Z")

</div>

Le 2 septembre 2006 à 11:47, Paul L. a écrit :

> > % ruby -v  
> > ruby 1.8.5 (2006-08-25) [powerpc-darwin8.7.0]  
> > (that is, MacOS X 10.4.7)
> 
> Okay, I think you are seeing a version-specific bug. Any other reader  
> comments?

I’ve made the test with ruby 1.8.4 : it works fine. Then, I upgraded to  
ruby 1.8.5 and it fails as described by the OP.

FWIW :

14:13 fred@talisker:~\> ruby -v  
ruby 1.8.5 (2006-08-25) [i386-freebsd5]

Fred

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/bignum-limits/67330/7 "2006-09-02T15:16:25Z")

</div>

F. Senault wrote:

> I’ve made the test with ruby 1.8.4 : it works fine. Then, I upgraded to  
> ruby 1.8.5 and it fails as described by the OP.
> 
> FWIW :
> 
> 14:13 fred@talisker:~\> ruby -v  
> ruby 1.8.5 (2006-08-25) [i386-freebsd5]
> 
> Fred

Thanks for the answers.  
Actually, it’s not a bug, it’s a feature 🙂

Here’s a piece of bignum.c, with “@” on the important lines:

* * *

VALUE  
rb\_big\_pow(x, y)  
VALUE x, y;  
{  
double d;  
long yy;

```
 if (y == INT2FIX(0)) return INT2FIX(1);
 switch (TYPE(y)) {
   case T_FLOAT:
     d = RFLOAT(y)->value;
     break;

```

@ case T\_BIGNUM:  
@ rb\_warn(“in a\*\*b, b may be too big”);  
@ d = rb\_big2dbl(y);  
break;

```
   case T_FIXNUM:
     yy = FIX2LONG(y);
     if (yy > 0) {
         VALUE z = x;

```

## @ if (RBIGNUM(x)-\>len \* SIZEOF\_BDIGITS \* yy \> 1024\*1024) { @ rb\_warn(“in a\*\*b, b may be too big”); @ d = (double)yy; break; }

Hence, 2^y when y is a bignum (that is, y \> 2^30 - 1), always  
raises a warning. And if y is a Fixnum, that depends on  
the size of the result (a warning when there is about 1 million  
binary digits). It’s rather annoying to get a float in that case…  
but I guess it’s supposed to prevent a very long computation (that  
cannot be interrupted by Ctrl-C) when there is a mistake in the  
program.

---

<div class="post-metadata">

**Author:** ![Jean-Claude\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@Jean-Claude\_A](https://www.ruby-forum.com/u/Jean-Claude_A)\
**Post date:** [September 2, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/bignum-limits/67330/8 "2006-09-02T16:28:29Z")

</div>

On 9/2/06, Jean-Claude A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hence, 2^y when y is a bignum (that is, y \> 2^30 - 1), always  
> raises a warning. And if y is a Fixnum, that depends on  
> the size of the result (a warning when there is about 1 million  
> binary digits). It’s rather annoying to get a float in that case…  
> but I guess it’s supposed to prevent a very long computation (that  
> cannot be interrupted by Ctrl-C) when there is a mistake in the  
> program.

And it’s really not a limit on Bignums in general, just on the argument  
to ^.

One limit WOULD be how much memory is available, but that’s going to  
be hardware dependent.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
