Integer#to_s(2) works on x86, fails on x86_64

Discussion in 'Ruby' started by Suraj Kurapati, Dec 5, 2006.

  1. Hello,

    Can anyone explain why this is happening?


    ---- on x86_64 ----

    $ ruby -v
    ruby 1.8.4 (2005-12-24) [x86_64-linux]

    $ irb
    >> 2956350890592680056.to_s(2)

    RangeError: integer 2956350890592680056 too big to convert to `int'
    from (irb):1:in `to_s'
    from (irb):1
    from :0


    ---- on x86 ----

    $ ruby -v
    ruby 1.8.4 (2005-12-24) [i486-linux]

    $ irb
    >> 2956350890592680056.to_s(2)

    => "10100100000111000100010111100010111001001011111011100001111000"


    Thanks for your consideration.

    --
    Posted via http://www.ruby-forum.com/.
    Suraj Kurapati, Dec 5, 2006
    #1
    1. Advertising

  2. Suraj Kurapati schrieb:
    > Hello,
    >
    > Can anyone explain why this is happening?


    no, but it works here:

    patrick@Dragon:~$ irb
    irb(main):001:0> 2956350890592680056.to_s(2)
    => "10100100000111000100010111100010111001001011111011100001111000"
    irb(main):002:0> quit
    patrick@Dragon:~$ ruby -v
    ruby 1.8.5 (2006-08-25) [x86_64-linux]

    maybe it helps to track the problem.

    thanks,
    patrick
    Patrick Plattes, Dec 5, 2006
    #2
    1. Advertising

  3. Paul Lutus wrote:
    > It's way too large to fit into an old-style integer of 32 bits, but it fits
    > with two bits to spare in a 64-bit integer. Also, in Ruby, normally an
    > integer too large for the Fixnum class is automatically converted to a
    > Bignum, so this error should not have happened.
    >
    > Try this test:
    >
    > 32.upto(128) do |p|
    > puts 2 ** p
    > end
    >
    > See what happens on both platforms. BTW I am thinking this is an error in
    > the x86_64 Ruby code.


    The same Exception is thrown on my machine.

    From cpuinfo:
    processor : 0
    vendor_id : AuthenticAMD
    cpu family : 15
    model : 47
    model name : AMD Athlon(tm) 64 Processor 3000+
    stepping : 2

    Operating System is Ubuntu 6.06 LTS.

    Ruby version (installed via OS package manager): ruby 1.8.4 (2005-12-24)
    [x86_64-linux].

    16.upto(128) do |p|
    n = 2 ** p
    puts("#{p}: #{n}: #{n.class}")
    end

    shows a conversion at

    61: 2305843009213693952: Fixnum
    62: 4611686018427387904: Bignum

    where to_s(2) fails at p == 31

    16.upto(128) do |p|
    n = 2 ** p
    puts("#{p}: #{n}: #{n.class}")
    puts(n.to_s(2))
    end

    31: 2147483648: Fixnum
    RangeError: integer 2147483648 too big to convert to `int'

    Stefan
    Stefan Mahlitz, Dec 5, 2006
    #3
  4. Suraj Kurapati

    Kalman Noel Guest

    Suraj Kurapati:
    > $ ruby -v
    > ruby 1.8.4 (2005-12-24) [x86_64-linux]
    >
    > $ irb
    >>> 2956350890592680056.to_s(2)

    > RangeError: integer 2956350890592680056 too big to convert to `int'


    irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    => "1.9.0 x86_64-linux 2006-10-16"
    irb> 2956350890592680056.to_s(2)
    => "10100100000111000100010111100010111001001011111011100001111000"

    irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    => "1.8.4 x86_64-linux 2005-12-24"
    irb> 2956350890592680056.to_s(2)
    RangeError: integer 2956350890592680056 too big to convert to `int'
    from (irb):2:in `to_s'
    from (irb):2

    Kalman
    Kalman Noel, Dec 5, 2006
    #4
  5. Suraj Kurapati

    Mihai Vlad Guest

    Kalman Noel wrote:
    > Suraj Kurapati:
    >
    >> $ ruby -v
    >> ruby 1.8.4 (2005-12-24) [x86_64-linux]
    >>
    >> $ irb
    >>
    >>>> 2956350890592680056.to_s(2)
    >>>>

    >> RangeError: integer 2956350890592680056 too big to convert to `int'
    >>

    >
    > irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    > => "1.9.0 x86_64-linux 2006-10-16"
    > irb> 2956350890592680056.to_s(2)
    > => "10100100000111000100010111100010111001001011111011100001111000"
    >
    > irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    > => "1.8.4 x86_64-linux 2005-12-24"
    > irb> 2956350890592680056.to_s(2)
    > RangeError: integer 2956350890592680056 too big to convert to `int'
    > from (irb):2:in `to_s'
    > from (irb):2
    >
    > Kalman
    >
    >


    Seems on 1.8.5 is ok aswell, so the problem is < 1.8.4

    irb(main):001:0> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    => "1.8.5 x86_64-linux 2006-12-04"
    irb(main):002:0> 2956350890592680056.to_s(2)
    => "10100100000111000100010111100010111001001011111011100001111000"


    --
    Andi
    Mihai Vlad, Dec 5, 2006
    #5
  6. Suraj Kurapati

    Ken Bloom Guest

    On Tue, 05 Dec 2006 23:24:57 +0900, Mihai Vlad wrote:

    > Kalman Noel wrote:
    >> Suraj Kurapati:
    >>
    >>> $ ruby -v
    >>> ruby 1.8.4 (2005-12-24) [x86_64-linux]
    >>>
    >>> $ irb
    >>>
    >>>>> 2956350890592680056.to_s(2)
    >>>>>
    >>> RangeError: integer 2956350890592680056 too big to convert to `int'
    >>>

    >>
    >> irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    >> => "1.9.0 x86_64-linux 2006-10-16"
    >> irb> 2956350890592680056.to_s(2)
    >> => "10100100000111000100010111100010111001001011111011100001111000"
    >>
    >> irb> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    >> => "1.8.4 x86_64-linux 2005-12-24"
    >> irb> 2956350890592680056.to_s(2)
    >> RangeError: integer 2956350890592680056 too big to convert to `int'
    >> from (irb):2:in `to_s'
    >> from (irb):2
    >>
    >> Kalman
    >>
    >>

    >
    > Seems on 1.8.5 is ok aswell, so the problem is < 1.8.4
    >
    > irb(main):001:0> "#{RUBY_VERSION} #{RUBY_PLATFORM} #{RUBY_RELEASE_DATE}"
    > => "1.8.5 x86_64-linux 2006-12-04"
    > irb(main):002:0> 2956350890592680056.to_s(2)
    > => "10100100000111000100010111100010111001001011111011100001111000"


    Yep. Here are the relevant changelog entries.

    Fri Aug 25 17:15:17 2006 Yukihiro Matsumoto <>

    * stable version 1.8.5 released.

    Sun Feb 5 21:05:34 2006 Hirokazu Yamamoto <>

    * numeric.c (fix_to_s): removed workaround for radix 2. Historically,
    rb_fix2str could only handle radix 8, 10, 16. (Rev1.37) But for now,
    it can handle radix 2..36. [ruby-Bugs#3438] [ruby-core:7300]
    Sat Feb 4 15:56:37 2006 Hirokazu Yamamoto <>

    * numeric.c (fix_to_s): (2**32).to_s(2) fails with exception where
    sizeof(int) == 4 < sizeof(long). [ruby-core:7300]

    Sat Dec 24 18:58:14 2005 Yukihiro Matsumoto <>

    * stable version 1.8.4 released.


    --
    Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
    Department of Computer Science. Illinois Institute of Technology.
    http://www.iit.edu/~kbloom1/
    Ken Bloom, Dec 5, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neal Becker

    Compile fails on x86_64

    Neal Becker, Sep 30, 2005, in forum: Python
    Replies:
    0
    Views:
    280
    Neal Becker
    Sep 30, 2005
  2. Neal Becker

    compile fails on x86_64 (more)

    Neal Becker, Sep 30, 2005, in forum: Python
    Replies:
    0
    Views:
    266
    Neal Becker
    Sep 30, 2005
  3. omkarenator

    stack pointer alignment on x86 and x86_64

    omkarenator, May 1, 2009, in forum: C Programming
    Replies:
    2
    Views:
    1,450
    omkarenator
    May 1, 2009
  4. Don
    Replies:
    60
    Views:
    1,355
    Phred Phungus
    Mar 19, 2010
  5. kazaam
    Replies:
    6
    Views:
    216
    kazaam
    Aug 27, 2007
Loading...

Share This Page