Infinity and -Infinity

Discussion in 'Ruby' started by Jon A. Lambert, Nov 5, 2005.

  1. Are there predefined constants somewhere for these floating point values?

    => ["EPSILON", "MIN_10_EXP", "MANT_DIG", "MAX", "MAX_EXP", "RADIX", "MIN",
    "MIN_EXP", "ROUNDS", "MAX_10_EXP", "DIG"]
    irb(main):011:0> Math.constants
    => ["E", "PI"]

    irb(main):016:0> Float::MAX
    => 1.79769313486232e+308
    irb(main):017:0> Float::MIN
    => 2.2250738585072e-308
    irb(main):009:0> (+1.0/0.0)
    => Infinity
    irb(main):012:0> (-1.0/0.0)
    => -Infinity

    irb(main):021:0> x = Inifinity
    NameError: uninitialized constant Inifinity
     
    Jon A. Lambert, Nov 5, 2005
    #1
    1. Advertising

  2. Jon A. Lambert

    daz Guest

    Jon A. Lambert wrote:
    > Are there predefined constants somewhere for these floating point values?
    >
    > => ["EPSILON", "MIN_10_EXP", "MANT_DIG", "MAX", "MAX_EXP", "RADIX", "MIN",
    > "MIN_EXP", "ROUNDS", "MAX_10_EXP", "DIG"]
    > irb(main):011:0> Math.constants
    > => ["E", "PI"]
    >
    > irb(main):016:0> Float::MAX
    > => 1.79769313486232e+308
    > irb(main):017:0> Float::MIN
    > => 2.2250738585072e-308



    That's a vexing question 'cos you just gave a comprehensive list of them.

    Perhaps numeric.c is the answer you wanted ?


    > irb(main):009:0> (+1.0/0.0)
    > => Infinity
    > irb(main):012:0> (-1.0/0.0)
    > => -Infinity
    >
    > irb(main):021:0> x = Inifinity
    > NameError: uninitialized constant Inifinity


    ( In<i>finity spelling irrelevant )

    Infinity is simulated all the way AFAICT - so, with your help,
    I think this shows that your definition usably sticks:

    negi = (-1.0/0.0)
    p negi # -Infinity
    Infinity = -negi
    p Infinity # Infinity
    p Infinity.class # Float
    p Infinity == (+1.0/0.0)-1 # true

    Without all that unnecessary fuss:

    Infinity = (+1.0/0.0) # set constant

    x = -Infinity-3
    p x # -Infinity


    daz
     
    daz, Nov 6, 2005
    #2
    1. Advertising

  3. daz wrote:
    >
    > Without all that unnecessary fuss:
    >
    > Infinity = (+1.0/0.0) # set constant
    >
    > x = -Infinity-3
    > p x # -Infinity
    >


    Okay thanks. I'm going to set my own constant.
    The math looks good too, although I'm just using for boundary conditions.

    I was suprised not to find min or max functions either

    looking for ...

    someclass#min(x,y)
    or
    x.min(y)

    I ended up using
    x < y ? x : y

    Then I stumbled upon...Enumerable's
    [x, y].min

    So I ended up using a mix of both as I was looping through arrays in some
    cases to get the min/max.
    I'm presuming the latter is slightly slower for just a check of two numbers.

    --
    J. Lambert
     
    Jon A. Lambert, Nov 6, 2005
    #3
  4. Jon A. Lambert

    Trans Guest

    Jon A. Lambert wrote:
    > I was suprised not to find min or max functions either
    >
    > looking for ...
    >
    > someclass#min(x,y)
    > or
    > x.min(y)
    >
    > I ended up using
    > x < y ? x : y


    It would be nice if there were, but there's a problem. Enumerables can
    be comparable too. So #min and #max would name clash with Enumerable's
    methods, and also with Date#min (for munutes). It still might be
    possible to do if one took arity into account though.

    As It is Florian Gross provided Facets with #at_least and #at_most,
    though to me those seem long winded for such a function. So I also
    offer #clip and #cap.

    4.cap(5) #=> 4
    4.cap(3) #=> 3

    4.clip(5) #=> 5
    4.clip(3) #=> 4

    4.clip(5,7) #=> 5
    4.clip(3,5) #=> 4
    4.clip(1,3) #=> 3

    T.
     
    Trans, Nov 6, 2005
    #4
    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. Bart Ogryczak

    pickle and infinity

    Bart Ogryczak, Nov 29, 2006, in forum: Python
    Replies:
    6
    Views:
    353
    Bart Ogryczak
    Nov 30, 2006
  2. Michael Hoffman

    Portably generating infinity and NaN

    Michael Hoffman, Apr 12, 2007, in forum: Python
    Replies:
    18
    Views:
    480
    Michael Hoffman
    Apr 16, 2007
  3. Jim Langston
    Replies:
    15
    Views:
    742
    James Kanze
    Oct 23, 2007
  4. migurus

    atoi and INFINITY

    migurus, Apr 15, 2008, in forum: C Programming
    Replies:
    1
    Views:
    547
    Owen Jacobson
    Apr 15, 2008
  5. Skybuck Flying

    Infinity + Infinity (or NegInfinity - NegInfinity)

    Skybuck Flying, Oct 8, 2011, in forum: C Programming
    Replies:
    40
    Views:
    1,715
    Tim Rentsch
    Jan 25, 2012
Loading...

Share This Page