문제

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