Infinity

Discussion in 'Ruby' started by hadley wickham, Jul 22, 2006.

  1. irb(main):084:0> (-1.0/0.0)
    => -Infinity
    irb(main):086:0> (1.0/0.0)
    => Infinity

    but

    irb(main):087:0> Infinity
    NameError: uninitialized constant Infinity
    from (irb):87
    from :0

    is it possible to directly access/create a float representing positive
    or negative infinity?

    Thanks,

    Hadley
     
    hadley wickham, Jul 22, 2006
    #1
    1. Advertising

  2. On Sun, Jul 23, 2006 at 04:22:37AM +0900, hadley wickham wrote:
    > irb(main):084:0> (-1.0/0.0)
    > => -Infinity
    > irb(main):086:0> (1.0/0.0)
    > => Infinity
    >
    > but
    >
    > irb(main):087:0> Infinity
    > NameError: uninitialized constant Infinity
    > from (irb):87
    > from :0
    >
    > is it possible to directly access/create a float representing positive
    > or negative infinity?


    Infinity = 1.0/0

    (N.B. Doesn't work on all platforms.)

    marcel
    --
    Marcel Molina Jr. <>
     
    Marcel Molina Jr., Jul 22, 2006
    #2
    1. Advertising

  3. > Infinity = 1.0/0
    >
    > (N.B. Doesn't work on all platforms.)


    But it will everywhere that supports the IEEE floating point standard?

    Thanks,

    Hadley
     
    hadley wickham, Jul 22, 2006
    #3
  4. Others have answered the main question here but it's worth noting for
    the future that just because it says infinity doesn't mean a constant
    exists with that name. Check this out:

    irb(main):001:0> class A; end
    => nil
    irb(main):002:0> class B; end
    => nil
    irb(main):003:0> A, B = B, A
    (irb):3: warning: already initialized constant A
    (irb):3: warning: already initialized constant B
    => [B, A]
    irb(main):004:0> A
    => B
    irb(main):005:0> B
    => A

    The name is independent of constants.

    hadley wickham wrote:
    > irb(main):084:0> (-1.0/0.0)
    > => -Infinity
    > irb(main):086:0> (1.0/0.0)
    > => Infinity
    >
    > but
    >
    > irb(main):087:0> Infinity
    > NameError: uninitialized constant Infinity
    > from (irb):87
    > from :0
    >
    > is it possible to directly access/create a float representing positive
    > or negative infinity?
    >
    > Thanks,
    >
    > Hadley
     
    Timothy Goddard, Jul 22, 2006
    #4
  5. On 7/22/06, Marcel Molina Jr. <> wrote:
    > Infinity = 1.0/0
    >
    > (N.B. Doesn't work on all platforms.)


    Does anyone know a definition for Infinity that does work on all
    platforms? Here's what I use; I have no idea how robust it really is:

    Infinity= begin
    result= [ Float::MAX**Float::MAX, Float::MAX**2, Float::MAX*2].max
    result.infinite? ? result : result=1.0/0
    rescue: Float::MAX #maybe 1.0/0 doesn't work on some systems?
    end unless defined? Infinity

    #there's also this way:
    999999999999999999999999999999999999999999999999e99999999999999999
    #but stuff like that sometimes returns zero, so it doesn't seem as reliable.
    #(plus it generates a warning)
     
    Caleb Clausen, Jul 23, 2006
    #5
  6. > Others have answered the main question here but it's worth noting for
    > the future that just because it says infinity doesn't mean a constant
    > exists with that name. Check this out:


    Yes, but a lot of languages provide some way of accessing the
    "constants" that represent special floating point numbers: Inf, -Inf
    and NaN

    Hadley
     
    hadley wickham, Jul 23, 2006
    #6
  7. hadley wickham wrote:
    >> Others have answered the main question here but it's worth noting for
    >> the future that just because it says infinity doesn't mean a constant
    >> exists with that name. Check this out:

    >
    > Yes, but a lot of languages provide some way of accessing the
    > "constants" that represent special floating point numbers: Inf, -Inf
    > and NaN

    I think Matz vetoed anything that required IEEE floating point a little
    while ago. So ... for Ruby code to use Inf, -Inf or NaN, there would
    need to be special care taken to maintain portability.
     
    M. Edward (Ed) Borasky, Jul 23, 2006
    #7
  8. > > Yes, but a lot of languages provide some way of accessing the
    > > "constants" that represent special floating point numbers: Inf, -Inf
    > > and NaN

    > I think Matz vetoed anything that required IEEE floating point a little
    > while ago. So ... for Ruby code to use Inf, -Inf or NaN, there would
    > need to be special care taken to maintain portability.


    Thanks for that explanation, although it does worry me a little, I
    understand that getting floating point working precisely correctly
    across multiple platforms is very hard.

    Regards,
    Hadley
     
    hadley wickham, Jul 23, 2006
    #8
  9. hadley wickham

    Trans Guest

    Facets has Infinity (actaully it was deprecated for a while until I
    fixed. Your post inspired me to fix it, so it's now back in the very
    latest version. Thanks!)

    It uses the constant INFINITY. Is that cool? Or is there some sort of
    general practice of using Inf?

    Thanks,
    T.
     
    Trans, Jul 23, 2006
    #9
  10. > Facets has Infinity (actaully it was deprecated for a while until I
    > fixed. Your post inspired me to fix it, so it's now back in the very
    > latest version. Thanks!)


    That's great! Thanks.

    > It uses the constant INFINITY. Is that cool? Or is there some sort of
    > general practice of using Inf?


    I think Inf is more common: R uses it, so does MATLAB
    (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inf.html),
    and there's a PEP for python that uses it
    (http://www.python.org/dev/peps/pep-0754/, which also has a nice
    discussion of these issues)

    Hadley
     
    hadley wickham, Jul 23, 2006
    #10
  11. hadley wickham

    Guest

    hadley wickham wrote:
    > > Facets has Infinity (actaully it was deprecated for a while until I
    > > fixed. Your post inspired me to fix it, so it's now back in the very
    > > latest version. Thanks!)

    >
    > That's great! Thanks.
    >
    > > It uses the constant INFINITY. Is that cool? Or is there some sort of
    > > general practice of using Inf?

    >
    > I think Inf is more common: R uses it, so does MATLAB
    > (http://www.mathworks.com/access/helpdesk/help/techdoc/ref/inf.html),
    > and there's a PEP for python that uses it
    > (http://www.python.org/dev/peps/pep-0754/, which also has a nice
    > discussion of these issues)


    Nice, that was helpful. I added some query methods to Numeric, eg.
    finite?, infinite?, ... and did this:

    UNDEFINED = InfinityClass.instance(0)
    INFINITY = InfinityClass.instance(1)

    NaN = UNDEFINED
    Inf = INFINITY
    PosInf = INFINITY
    NegInf = -INFINITY

    Thanks!
    T.
     
    , Jul 23, 2006
    #11
  12. hadley wickham wrote:
    >> > Yes, but a lot of languages provide some way of accessing the
    >> > "constants" that represent special floating point numbers: Inf, -Inf
    >> > and NaN

    >> I think Matz vetoed anything that required IEEE floating point a little
    >> while ago. So ... for Ruby code to use Inf, -Inf or NaN, there would
    >> need to be special care taken to maintain portability.

    >
    > Thanks for that explanation, although it does worry me a little, I
    > understand that getting floating point working precisely correctly
    > across multiple platforms is very hard.

    Which is *precisely* why dozens of hard core applied mathematicians and
    computer scientists, the IEEE and SIAM (Society for Industrial and
    Applied Mathematics), and nearly every vendor of equipment intended for
    scientific computing, both large and small, developed and embraced the
    IEEE standards! Note that there are actually *two* IEEE standards. The
    one most of us think of is the 32-bit/64-bit one that's implemented in
    Intel hardware. However, there was another looser standard which did not
    specify bit for bit behavior but only the computational requirements.
    The IEEE floating point standards are happy exceptions to the rule that
    nothing good was ever designed by a committee. :)

    Some important vendors of such equipment -- Cray, FPS and IBM -- adhered
    to neither of these standards. Digital/Compaq/HP adopted it for the
    Alphas but kept the Vax line at its historic format for compatibility.
    But *new* architectures almost entirely adopted IEEE -- there is little
    reason not to do so. At some point in the evolution of Ruby, the number
    of actual users of non-IEEE platforms will be negligible. Just out of
    curiosity, how many users of non-IEEE Ruby are there on this list, and
    what fraction of the total list membership does that represent? Feel
    free to decline to answer if you're in a classified shop. :)
     
    M. Edward (Ed) Borasky, Jul 23, 2006
    #12
  13. > Nice, that was helpful. I added some query methods to Numeric, eg.
    > finite?, infinite?, ... and did this:
    >
    > UNDEFINED = InfinityClass.instance(0)
    > INFINITY = InfinityClass.instance(1)
    >
    > NaN = UNDEFINED
    > Inf = INFINITY
    > PosInf = INFINITY
    > NegInf = -INFINITY


    Great. Note that (NaN == NaN) == False, but I think the rest of the
    relations are straightforward.

    Hadley
     
    hadley wickham, Jul 23, 2006
    #13
  14. hadley wickham

    Tanaka Akira Guest

    In article <>,
    "M. Edward (Ed) Borasky" <> writes:

    > Some important vendors of such equipment -- Cray, FPS and IBM -- adhered
    > to neither of these standards. Digital/Compaq/HP adopted it for the
    > Alphas but kept the Vax line at its historic format for compatibility.
    > But *new* architectures almost entirely adopted IEEE -- there is little
    > reason not to do so. At some point in the evolution of Ruby, the number
    > of actual users of non-IEEE platforms will be negligible. Just out of
    > curiosity, how many users of non-IEEE Ruby are there on this list, and
    > what fraction of the total list membership does that represent? Feel
    > free to decline to answer if you're in a classified shop. :)


    Some time ago, I installed Ruby on NetBSD/vax on SIMH.
    http://www.netbsd.org/Ports/vax/
    http://simh.trailing-edge.com/

    It is fine until make.
    But make test fails because 0.0/0 fails.
    I guess VAX has no NaN.

    I think no one try to fix it.

    Note that I also installed Ruby on Debian for S/390 on Hercules.
    http://www.debian.org/ports/s390/
    http://www.conmicro.cx/hercules/

    Unexpectedly, Ruby works fine on S/390.

    After some investigation, I found that newer models have
    IEEE registers and Linux kernel emulates them for old
    models.
    http://www.linuxbase.org/spec/ELF/zSeries/lzsabi0_s390.html
    --
    Tanaka Akira
     
    Tanaka Akira, Jul 23, 2006
    #14
    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. a_Conan
    Replies:
    3
    Views:
    536
    a_Conan
    Sep 8, 2005
  2. Sidney Cadot

    integer division towards -infinity

    Sidney Cadot, Jul 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    635
    Glen Herrmannsfeldt
    Jul 11, 2003
  3. Anand Pillai

    Queues - Is Infinity all right?

    Anand Pillai, Oct 5, 2003, in forum: Python
    Replies:
    9
    Views:
    317
    Ype Kingma
    Oct 8, 2003
  4. Skybuck Flying

    Infinity + Infinity (or NegInfinity - NegInfinity)

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

    Infinity and -Infinity

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

Share This Page