Ruby max integer

后端 未结 6 1254
北海茫月
北海茫月 2020-11-29 20:21

I need to be able to determine a systems maximum integer in Ruby. Anybody know how, or if it\'s possible?

6条回答
  •  佛祖请我去吃肉
    2020-11-29 20:39

    In ruby Fixnums are automatically converted to Bignums.

    To find the highest possible Fixnum you could do something like this:

    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)
    

    Shamelessly ripped from a ruby-talk discussion. Look there for more details.

提交回复
热议问题