RangeError:bignum太大,无法转换为“ long” [英] RangeError: bignum too big to convert into `long'

查看:245
本文介绍了RangeError:bignum太大,无法转换为“ long”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  num = 0000001000000000011000000000000010010011000011110000000000000000 
for n in 0 ... num.length
temp = num [n]
dec = dec + temp *(2 **(数字长度-n-1))
结束
卖出dec

当我在irb中运行此代码时,输​​出以下错误消息。当我在python中编译相同的逻辑时,它的工作绝对正常。我在Google上搜索了 RangeError:bignum太大,无法转换为'long':但未找到相关答案。
请帮助我:(预先感谢。



 
RangeError:bignum太大,无法转换为 long'
from(irb):4:in
*'
from(irb):4:in irb_binding中的块'
from(irb):2:in
每个'
from(irb ):2
来自C:/ Ruby193 / bin / irb:12:in''

解决方案

尝试一下

  num = 0000001000000000011000000000000010010011000011110000000000000000 
dec = 0
for n in 0 ... num.length
temp = num [n]
dec = dec + temp.to_i *(2 **(numlength-n-1))
end
put dec


num = "0000001000000000011000000000000010010011000011110000000000000000"
for n in 0...num.length 
   temp = num[n] 
   dec =  dec + temp*(2**(num.length - n - 1))
end
puts dec

When i am running this code in irb the following error message is the output. and when i compiled the same logic in python it is working absolutely fine. I have Googled "RangeError: bignum too big to convert into `long': but didn't find the relevant answer. Please help me :( Thanks in Advance.

RangeError: bignum too big to convert into long'
        from (irb):4:in*'
        from (irb):4:in block in irb_binding'
        from (irb):2:ineach'
        from (irb):2
        from C:/Ruby193/bin/irb:12:in `'

解决方案

Try this

num = "0000001000000000011000000000000010010011000011110000000000000000"
dec = 0
for n in 0...num.length 
   temp = num[n] 
   dec =  dec + temp.to_i * (2**(num.length - n - 1))
end
puts dec

这篇关于RangeError:bignum太大,无法转换为“ long”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆