我需要能够确定Ruby中的系统最大整数.有人知道怎么样,或者有可能吗?
FIXNUM_MAX = (2**(0.size * 8 -2) -1) FIXNUM_MIN = -(2**(0.size * 8 -2))
Ruby在溢出时自动将整数转换为大整数类,因此(实际上)它们的大小没有限制.
如果您正在寻找机器的大小,即64位或32位,我在ruby-forum.com上找到了这个技巧:
machine_bytes = ['foo'].pack('p').size machine_bits = machine_bytes * 8 machine_max_signed = 2**(machine_bits-1) - 1 machine_max_unsigned = 2**machine_bits - 1
如果您正在寻找Fixnum对象的大小(小到足以存储在单个机器字中的整数),您可以调用0.size
以获取字节数.我猜它应该是32位版本的4,但我现在无法测试.此外,最大的Fixnum显然是2**30 - 1
(或2**62 - 1
),因为一位用于将其标记为整数而不是对象引用.
阅读友好的手册?谁想要这样做?
start = Time.now largest_known_fixnum = 1 smallest_known_bignum = nil until smallest_known_bignum == largest_known_fixnum + 1 if smallest_known_bignum.nil? next_number_to_try = largest_known_fixnum * 1000 else next_number_to_try = (smallest_known_bignum + largest_known_fixnum) / 2 # Geometric mean would be more efficient, but more risky end if next_number_to_try <= largest_known_fixnum || smallest_known_bignum && next_number_to_try >= smallest_known_bignum raise "Can't happen case" end case next_number_to_try when Bignum then smallest_known_bignum = next_number_to_try when Fixnum then largest_known_fixnum = next_number_to_try else raise "Can't happen case" end end finish = Time.now puts "The largest fixnum is #{largest_known_fixnum}" puts "The smallest bignum is #{smallest_known_bignum}" puts "Calculation took #{finish - start} seconds"
在ruby Fixnums中自动转换为Bignums.
要找到最高可能的Fixnum,您可以执行以下操作:
class Fixnum N_BYTES = [42].pack('i').size N_BITS = N_BYTES * 8 MAX = 2 ** (N_BITS - 2) - 1 MIN = -MAX - 1 end p(Fixnum::MAX)
从一个红宝石谈话讨论中无耻地撕掉了.看那里了解更多细节.