Adding Infinity and NaN to Kernel (#4)

Submitted by: Armin Ronacher

This revision by: Armin Ronacher

Date: Wed Jan 10 10:11:50 -0500 2007

ABSTRACT

Adding Infinty and NaN constants to Kernel.

PROBLEM

As proposes Ruby supports ‘Infinity’ and ‘NaN’ and provides a working inspect for that (Infinity, -Infinity and NaN).

But those constants do not exist on `Kernel`.

PROPOSAL

irb(main):001:0> NaN
=> NaN
irb(main):002:0> Infinity
=> Infinity
irb(main):003:0> -Infinity
=> -Infinity

ANALYSIS

Pro: – less confusing

Cons: – more people would find out that there are Infinity and NaN and probably wonder why NaN == NaN is false

IMPLEMENTATION

module Kernel
  Infinity = 0.0 / 0.0
  NaN = 0.0 / 0.0
end

Comments

from Pit Capitain, Thu Aug 09 08:30:50 -0400 2007

I wouldn't introduce the constants in the global Namespace, but in the 
Math Module. Otherwise I'm in favor of this.

from Shyouhei Urabe, Sat Sep 01 01:41:15 -0400 2007

It's not easy because there is no portable way to generate NaNs and
Infs. 0.0/0.0 in your environment may perhaps generate a NaN, but
not always.

from Gavin Kistner, Sat Sep 01 10:24:57 -0400 2007

On Aug 31, 2007, at 11:41 PM, shyouhei@ruby-lang.org wrote:
> It's not easy because there is no portable way to generate NaNs and
> Infs. 0.0/0.0 in your environment may perhaps generate a NaN, but
> not always.

In what environment(s) does it work, and in what environment(s) does  
it not? (If not a comprehensive list, then at least a few key examples.)


from Shyouhei Urabe, Sat Sep 01 11:05:46 -0400 2007

phrogz@mac.com wrote:
> In what environment(s) does it work, and in what environment(s) does  
> it not? (If not a comprehensive list, then at least a few key examples.)

This was once discussed in Japanese ruby-dev ML (google these for
detail: [ruby-dev:4867], [ruby-dev:4603]), and at least
* old Sun cc
* VC++ 6.0
are known to stop compiling 0.0/0.0, saying "division by zero".

Also known is that binaries complied by
* egcs-2.91.60 on FreeBSD 2.2.8-STABLE
will dump cores on the 0.0/0.0 line, saying "Floating exception".

And the worst thing is that
* Watcom C
is known to generate +Inf when it evaluates 0.0/0.0 .

from Paul Brannan, Tue Sep 04 08:50:40 -0400 2007

On Sat, Sep 01, 2007 at 11:05:49AM -0400, shyouhei@ruby-lang.org wrote:
> phrogz@mac.com wrote:
> > In what environment(s) does it work, and in what environment(s) does  
> > it not? (If not a comprehensive list, then at least a few key examples.)
> 
> This was once discussed in Japanese ruby-dev ML (google these for
> detail: [ruby-dev:4867], [ruby-dev:4603]), and at least
> * old Sun cc
> * VC++ 6.0
> are known to stop compiling 0.0/0.0, saying "division by zero".

I believe this can be worked around by using a temporary variable.

> Also known is that binaries complied by
> * egcs-2.91.60 on FreeBSD 2.2.8-STABLE
> will dump cores on the 0.0/0.0 line, saying "Floating exception".

Does this happen if SIGFPE is caught/ignored?

> And the worst thing is that
> * Watcom C
> is known to generate +Inf when it evaluates 0.0/0.0 .

The NAN and INFINITY macros can be used, where available.  These are
C99, which I think is available on most platforms these days.

HUGE_VAL can also be used, which should be +Inf on platforms that
support Infinity.

Note that not all platforms may support Inf or NaN, only those platforms
that support IEEE floating point arithmetic.

On platforms where Nan/Inf cannot be generated or where they are
otherwise unavailable, don't add the constants to the Kernel.  Anyone
writing software for these platforms that uses floating-point arithmetic
probably has other portability issues to deal with as well.

Paul



Return to top

Copyright © 2006, Ruby Power and Light, LLC