3

I'm trying to calculate 2^1000 in ruby, but I'm getting different results for different versions. Any idea why?

Using 1.8.7:

~ » rbenv global system                                                                         ~  
~ » ruby --version                                                                              ~  
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
~ » irb                                                                                         ~  
>> 2 ** 1000
0
>> exit

Using ruby 1.9.3

~ » rbenv global 1.9.3-p194                                                                     ~  
~ » ruby --version                                                                              ~  
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.1.0]
~ » irb                                                                                         ~  
>> 2 ** 1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
>> exit

I can get around using that syntax and have it work, but I'm just curious why it results in 0 in 1.8.7.

mportiz08
  • 10,206
  • 12
  • 40
  • 42
  • With `ruby 1.8.7 (2012-10-12 patchlevel 371) [i686-darwin12.2.0]` on my machine, the value is correct. – Prakash Murthy Feb 12 '13 at 04:13
  • I am unable to reproduce your odd 1.8.7 behavior. Running under rvm, and using 1.8.7-p358, I get the same results as 1.9.3-p125. – Wayne Conrad Feb 12 '13 at 04:13
  • 6
    It might be a bug in that version of 1.8.7, the same thing happens on my Mac using the system ruby. Maybe it's related to this http://stackoverflow.com/questions/12009799/exponentiation-in-ruby-1-8-7-returns-wrong-answers – Peter Brown Feb 12 '13 at 04:15

1 Answers1

0

I am reasonaly certain that you are experiencing the bug mentioned in this post.

It appears as though it was fixed by this commit to Ruby trunk, which made it to Ruby 1.9.3-p0 and greater.

Community
  • 1
  • 1
Nathan Kleyn
  • 5,103
  • 3
  • 32
  • 49