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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top