N00b question about Infinity and Float

Discussion in 'Ruby' started by Kedar Mhaswade, Dec 31, 2008.

  1. Folks,

    A new user. Please be kind. I have checked
    http://www.ruby-forum.com/topic/74141#new, but could not find what I was
    looking for.

    See following irb session:

    start

    irb(main):004:0* google=10**100
    =>
    10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    irb(main):005:0> googleplex=10**google
    (irb):5: warning: in a**b, b may be too big
    => Infinity
    irb(main):006:0> google.class
    => Bignum
    irb(main):007:0> googleplex.class
    => Float

    end

    This was nice! But, I expected googleplex.class to be BigNum. Is
    Infinity a Float?

    Why?

    Thanks in advance,
    Kedar
    --
    Posted via http://www.ruby-forum.com/.
     
    Kedar Mhaswade, Dec 31, 2008
    #1
    1. Advertising

  2. Hi,

    Am Mittwoch, 31. Dez 2008, 12:12:55 +0900 schrieb Kedar Mhaswade:
    > irb(main):004:0* google=10**100
    > =>
    > 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    > irb(main):005:0> googleplex=10**google
    > (irb):5: warning: in a**b, b may be too big
    > => Infinity
    > irb(main):006:0> google.class
    > => Bignum
    > irb(main):007:0> googleplex.class
    > => Float
    >
    > This was nice! But, I expected googleplex.class to be BigNum. Is
    > Infinity a Float?


    I like this kind of questions. A weird example, though; I would
    prefer an exception. BigNum * BigNum may not result in a Float.
    Hit me with your refutations.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
     
    Bertram Scharpf, Dec 31, 2008
    #2
    1. Advertising

  3. Bertram Scharpf wrote:
    > Hi,
    >
    > Am Mittwoch, 31. Dez 2008, 12:12:55 +0900 schrieb Kedar Mhaswade:
    >> irb(main):004:0* google=10**100
    >> =>
    >> 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    >> irb(main):005:0> googleplex=10**google
    >> (irb):5: warning: in a**b, b may be too big
    >> => Infinity
    >> irb(main):006:0> google.class
    >> => Bignum
    >> irb(main):007:0> googleplex.class
    >> => Float
    >>
    >> This was nice! But, I expected googleplex.class to be BigNum. Is
    >> Infinity a Float?

    >
    > I like this kind of questions. A weird example, though; I would
    > prefer an exception. BigNum * BigNum may not result in a Float.
    > Hit me with your refutations.
    >
    > Bertram
    >
    >

    Hmmm ... "google" is a Bignum (multi-precision integer). "10" is an
    Integer. So you are raising an Integer to a Bignum power. So it boils
    down to how Ruby raises an Integer to a Bignum power. Now if this were
    an "ideal machine", it would perform some kind of exact, and very long
    running, exponentiation routine. Clearly it didn't ... it looked at the
    numbers and said, "Whoa!" :)

    Yeah ... it should have returned an exception, not a Float. But there
    are plus and minus Infinity in the IEEE floating point number spec, so I
    guess it makes sense to return that. But in theory, given enough memory
    for the digits and enough compute time, it could return a Bignum. An
    estimate of the amount of memory required is left as an exercise for the
    student. :)

    Incidentally, the correct spelling is "googol", not to be confused with
    the search engine or the Russian author. :)

    --
    M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P), WOM

    I've never met a happy clam. In fact, most of them were pretty steamed.
     
    M. Edward (Ed) Borasky, Dec 31, 2008
    #3
    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. bd
    Replies:
    0
    Views:
    654
  2. Jim Langston
    Replies:
    15
    Views:
    748
    James Kanze
    Oct 23, 2007
  3. Carsten Fuchs
    Replies:
    45
    Views:
    1,617
    James Kanze
    Oct 8, 2009
  4. Skybuck Flying

    Infinity + Infinity (or NegInfinity - NegInfinity)

    Skybuck Flying, Oct 8, 2011, in forum: C Programming
    Replies:
    40
    Views:
    1,733
    Tim Rentsch
    Jan 25, 2012
  5. Jon A. Lambert

    Infinity and -Infinity

    Jon A. Lambert, Nov 5, 2005, in forum: Ruby
    Replies:
    3
    Views:
    348
    Trans
    Nov 6, 2005
Loading...

Share This Page