NaN and Inifinity

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

Scott

I was able to do:

irb(main):012:0> a = Math::log(-1)
=> NaN
irb(main):013:0> a.class
=> Float
irb(main):014:0> b = “Na”.to_f
=> 0.0
irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

-Kurt

···

On Tue, Aug 12, 2003 at 10:38:40AM +0900, Scott Thompson wrote:

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

Scott

======= End of Original Message =======<

In article B0D86431-CC65-11D7-AFAD-000393803090@mac.com,

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

I don’t understand what you mean by ‘assign the variable “a” to NaN’.

NaNs and infinite values are best tested for using the nan? and
infinite? methods. Note that infinite? returns +1 or -1 for positive
and negative infinities and nil for finite numbers:

a = 0.0/0.0
=> NaN
a.nan?
=> true
a.infinite?
=> nil
b = 0.0/0.0
=> NaN
b.nan?
=> true
a == b
=> false
(+1/0.0).infinite?
=> 1
(-1/0.0).infinite?
=> -1
1.0.infinite?
=> nil
1 / (-1/0.0)
=> -0.0

I’m avoiding the mathematical justification for these behaviours, they’re
just what IEEE 754 math does. There are plenty of references on the web

Mike

···

Scott Thompson easco@mac.com wrote:

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

In article B0D86431-CC65-11D7-AFAD-000393803090@mac.com,

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

If you are using IEEE 754 math then you can try

[mike@ratdog mike]$ irb --simple-prompt

negInf = [‘FFF0000000000000’].pack('H’).unpack(‘G’)
=> -Infinity
inf = [‘7FF0000000000000’].pack('H’).unpack(‘G’)
=> Infinity
negInf.infinite?
=> -1
nan = [‘7FF8000000000000’].pack('H’).unpack(‘G’)
=> NaN
nan.nan?
=> true
nan == nan
=> false

(though there are a lot of ways to initialise a nan…)

Once you have them in a variable then there’s not much you can do with
them.

Hope this helps,

Mike

···

Scott Thompson easco@mac.com wrote:


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Hi Scott.

It might help to think of NaN meaning “some undefined value”. Then it
makes sense that you can’t assign it - it’s undefined - and also that
you can’t compare two NaNs - they’re both undefined so how can you? All
you can do is use Float.nan? to ask whether the result of what you just
did, in this case log(-1), is undefined. It also helps explain why it
isn’t, um, defined in Math or Float.

Cheers,
Dan

Scott Thompson wrote:

···

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant
in Float or Math.

Scott

I apparently was also able to paste too much of my session in here to be
relevant. Ignore the first three irb commands.

-Kurt

···

On Tue, Aug 12, 2003 at 10:53:34AM +0900, Kurt M. Dresner wrote:

I was able to do:

irb(main):012:0> a = Math::log(-1)
=> NaN
irb(main):013:0> a.class
=> Float
irb(main):014:0> b = “Na”.to_f
=> 0.0
irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

-Kurt

On Tue, Aug 12, 2003 at 10:38:40AM +0900, Scott Thompson wrote:

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

Scott

======= End of Original Message =======<

======= End of Original Message =======<

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

Scott

In article B0D86431-CC65-11D7-AFAD-000393803090@mac.com,

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

module Math
Nan = log(-1)
end

Now it is. :slight_smile:

Hal

···

----- Original Message -----
From: “Mike Stok” mike@ratdog.stok.co.uk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 11, 2003 9:21 PM
Subject: Re: NaN and Inifinity

Scott Thompson easco@mac.com wrote:

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant
in
Float or Math.

I don’t understand what you mean by ‘assign the variable “a” to NaN’.

NaNs and infinite values are best tested for using the nan? and
infinite? methods. Note that infinite? returns +1 or -1 for positive
and negative infinities and nil for finite numbers:

In the C library that I am trying to expose to Ruby there is a constant
for a NULL rectangle. You would get a NULL rectangle, for example, if
you asked for the intersection of two disjoint rectangles. The NULL
rectangle is very different from the “zero rectangle” (a rectangle
whose corner is at the origin and whose height and width are zero).

The NULL rectangle is implemented as a rectangle whose lower left
corner is at (+Infinity, +Infinity) and whose height and width are both
zero.

I wanted to make sure that if I exposed that constant to Ruby that it
would behave as expected. As a result, I was trying to experiment with
“irb” and I wanted to create an instance of my “Point” object whose x
and y coordinates were +Infinity. I was looking to do something like:

apoint = CGPoint.new(Float::Infinity, Float::Infinity)

when I couldn’t find Infinity I started looking for NaN as well. I
found some sample code on the internet that showed how to generate a
NaN result, but not how to initialize a float to NaN or +/-Infinity.

It seems, in some cases, it might be valuable to assign a float to “a
value that is not a legal value” similar to the way one might assign a
pointer to NULL in C, or even a object reference to nil in Ruby. But
that may be just me :slight_smile:

Scott

Hi Scott. It might help to think of NaN meaning “some
undefined value”. Then it makes sense that you can’t assign it

  • it’s undefined - and also that you can’t compare two NaNs -
    they’re both undefined so how can you? All you can do is use
    Float.nan? to ask whether the result of what you just did, in
    this case log(-1), is undefined. It also helps explain why it
    isn’t, um, defined in Math or Float.
> Cheers, Dan

In statistics, there’s a big difference between zero and no data, and
it’s nice to be able to initialize variables to indicate the latter.
NaN seems appropriate for this, so it would be nice if it were readily
available instead of having to be manufactured.

–paul

Scott Thompson wrote:

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

Not that wacky. I get the same.

C:> ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]

Maybe this has changed recently?

Scott Thompson wrote:

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

Scott

It’s not just you. Happens to me, too, using Ruby 1.8.0pre9 (I believe)
mswin32 build.

Then again, Math::log(-1) throws a domain error for me.

If worse comes to worst you could always initialize with Math::log(-1),
since that returns
what you want.

  • Dan

I get the same thing. I think it’s something wacky with 1.8.

-Mark

···

On Tue, Aug 12, 2003 at 11:06:51AM +0900, Scott Thompson wrote:

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

The other thing you might want to consider is how useful this would be.
You can’t compare NaNs, as far as I know.

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> a == b
=> false
irb(main):020:0> a
=> NaN
irb(main):021:0> b
=> NaN
irb(main):022:0> a == a
=> false

-Kurt

···

On Tue, Aug 12, 2003 at 11:26:58AM +0900, Hal E. Fulton wrote:

----- Original Message -----
From: “Mike Stok” mike@ratdog.stok.co.uk
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 11, 2003 9:21 PM
Subject: Re: NaN and Inifinity

In article B0D86431-CC65-11D7-AFAD-000393803090@mac.com,
Scott Thompson easco@mac.com wrote:

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN (or
Inifinity or -Infinity) to begin with? I tried finding “NaN” as a
constant but couldn’t locate it. It doesn’t appear to be a constant in
Float or Math.

module Math
Nan = log(-1)
end

Now it is. :slight_smile:

Hal

======= End of Original Message =======<

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN
(or Inifinity or -Infinity) to begin with? I tried finding
“NaN” as a constant but couldn’t locate it. It doesn’t appear
to be a constant in Float or Math.
I don’t understand what you mean by ‘assign the variable “a”
to NaN’.

NaNs and infinite values are best tested for using the nan? and
infinite? methods. Note that infinite? returns +1 or -1 for
positive and negative infinities and nil for finite numbers:

> In the C library that I am trying to expose to Ruby there is a
> constant for a NULL rectangle.  You would get a NULL
> rectangle, for example, if you asked for the intersection of
> two disjoint rectangles.  The NULL rectangle is very different
> from the "zero rectangle" (a rectangle whose corner is at the
> origin and whose height and width are zero).

> The NULL rectangle is implemented as a rectangle whose lower
> left corner is at (+Infinity, +Infinity) and whose height and
> width are both zero.

> I wanted to make sure that if I exposed that constant to Ruby
> that it would behave as expected.  As a result, I was trying
> to experiment with "irb" and I wanted to create an instance of
> my "Point" object whose x and y coordinates were +Infinity.  I
> was looking to do something like:

> apoint = CGPoint.new(Float::Infinity, Float::Infinity)

> when I couldn't find Infinity I started looking for NaN as
> well.  I found some sample code on the internet that showed
> how to generate a NaN result, but not how to initialize a
> float to NaN or +/-Infinity.

> It seems, in some cases, it might be valuable to assign a
> float to "a value that is not a legal value" similar to the
> way one might assign a pointer to NULL in C, or even a object
> reference to nil in Ruby. But that may be just me :-)

I wanted the same thing for a simple statistics class that used Kalman
filter-style updates instead of the more traditional (and prone to
numerical problems) sum of x, sum of x^2 based calculations. Like
you, I wanted to distinguish between the case where I had no data and
the case where I had zero for mean, variance, min, or max. I did the
following:

class SimpleStats
attr_reader :n, :mean, :variance, :min, :max

def initialize
reset
end

def reset
@n = 0
@variance = @mean = Math::log(-1)
@min = 1.0 / 0.0
@max = -@min
end

def newObs(x)
@max = x if x > @max
@min = x if x < @min
@n += 1
if (@n > 1)
x -= @mean
@mean += x / @n
@variance = (@n - 2.0) / (@n - 1.0) * @variance + x * x / @n
else
@mean = x
@variance = 0.0
end
end

def stdDeviation
Math::sqrt @variance
end
end

Note that by initializing min and max to +/- Infinity, respectively,
the update stage for them doesn’t need a special case for the first
time. If only the same were true of the mean and variance updates…

If anyone has any suggestions for tightening this up, by the way,
they’d be appreciated!

–paul

Oh yeah, I’m way out of date.

I think I’m using 1.6.something.

-Kurt

···

On Tue, Aug 12, 2003 at 11:16:41AM +0900, Dan Doel wrote:

Scott Thompson wrote:

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

Scott

It’s not just you. Happens to me, too, using Ruby 1.8.0pre9 (I believe)
mswin32 build.

Then again, Math::log(-1) throws a domain error for me.

If worse comes to worst you could always initialize with Math::log(-1),
since that returns
what you want.

  • Dan

======= End of Original Message =======<

Dan Doel wrote:

Scott Thompson wrote:

irb(main):015:0> a = Math::log(-1)
=> NaN
irb(main):016:0> a.class
=> Float
irb(main):017:0> b = “NaN”.to_f
=> NaN
irb(main):018:0> b.class
=> Float
irb(main):019:0> b.nan?
=> true

Hrrrm… Okee dokey. I tried something like that and I get

irb(main):002:0> b = “NaN”.to_f
=> 0.0
irb(main):003:0> b.nan?
=> false

There must be something wacky with my Ruby. :slight_smile:

Scott

It’s not just you. Happens to me, too, using Ruby 1.8.0pre9 (I
believe) mswin32 build.

Then again, Math::log(-1) throws a domain error for me.

If worse comes to worst you could always initialize with
Math::log(-1), since that returns
what you want.

  • Dan

I got one for you. I was getting this:

irb(main):001:0> a = Math::log(-1)
=> 3.141592653589793i
irb(main):002:0> a.class
=> Complex
irb(main):003:0> b = “NaN”.to_f
=> 0.0
irb(main):004:0> b.class
=> Float
irb(main):005:0> b.nan?
=> false

Then I realized that I start irb with the -m option. Hmmm.

You’re probably right. Besides that, I committed
three other sins in that little post – used a
misleading attribution, made a typo, and ignored
the Complex issue.

Well, it was free advice, and sometimes you get
what you pay for…

Hal

···

----- Original Message -----
From: “Kurt M. Dresner” kdresner@cs.utexas.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 11, 2003 9:29 PM
Subject: Re: NaN and Inifinity

The other thing you might want to consider is how useful this would be.
You can’t compare NaNs, as far as I know.

In article 86r83r5v8l.fsf@daemon.argelfraster.org,

If I do something like

a = Math::log(-1)

then a properly takes the value NaN.

Is there any way to explicitly assign the variable “a” to NaN
(or Inifinity or -Infinity) to begin with? I tried finding
“NaN” as a constant but couldn’t locate it. It doesn’t appear
to be a constant in Float or Math.
I don’t understand what you mean by ‘assign the variable “a”
to NaN’.

NaNs and infinite values are best tested for using the nan? and
infinite? methods. Note that infinite? returns +1 or -1 for
positive and negative infinities and nil for finite numbers:

In the C library that I am trying to expose to Ruby there is a
constant for a NULL rectangle. You would get a NULL
rectangle, for example, if you asked for the intersection of
two disjoint rectangles. The NULL rectangle is very different
from the “zero rectangle” (a rectangle whose corner is at the
origin and whose height and width are zero).

The NULL rectangle is implemented as a rectangle whose lower
left corner is at (+Infinity, +Infinity) and whose height and
width are both zero.

I wanted to make sure that if I exposed that constant to Ruby
that it would behave as expected. As a result, I was trying
to experiment with “irb” and I wanted to create an instance of
my “Point” object whose x and y coordinates were +Infinity. I
was looking to do something like:

apoint = CGPoint.new(Float::Infinity, Float::Infinity)

when I couldn’t find Infinity I started looking for NaN as
well. I found some sample code on the internet that showed
how to generate a NaN result, but not how to initialize a
float to NaN or +/-Infinity.

It seems, in some cases, it might be valuable to assign a
float to “a value that is not a legal value” similar to the
way one might assign a pointer to NULL in C, or even a object
reference to nil in Ruby. But that may be just me :slight_smile:

I wanted the same thing for a simple statistics class that used Kalman
filter-style updates instead of the more traditional (and prone to
numerical problems) sum of x, sum of x^2 based calculations. Like
you, I wanted to distinguish between the case where I had no data and
the case where I had zero for mean, variance, min, or max. I did the
following:

class SimpleStats
attr_reader :n, :mean, :variance, :min, :max

def initialize
reset
end

def reset
@n = 0
@variance = @mean = Math::log(-1)
@min = 1.0 / 0.0
@max = -@min
end

def newObs(x)
@max = x if x > @max
@min = x if x < @min
@n += 1
if (@n > 1)
x -= @mean
@mean += x / @n
@variance = (@n - 2.0) / (@n - 1.0) * @variance + x * x / @n
else
@mean = x
@variance = 0.0
end
end

def stdDeviation
Math::sqrt @variance
end
end

Note that by initializing min and max to +/- Infinity, respectively,
the update stage for them doesn’t need a special case for the first
time. If only the same were true of the mean and variance updates…

If anyone has any suggestions for tightening this up, by the way,
they’d be appreciated!

Does it make sense that

s = SimpleStats.new
s.max => Infinity

nil seems more reasonable for a “no value” result.

If you let @variance, @mean, @max and @min start out life as nil then

def newObs(x)
@max = [@max, x].max
@min = [@min, x].min
@n += 1

unless @mean # if @mean.nil?
@mean, @variance = x, 0.0
else
x -= @mean
@mean += x / @n
@variance = (@n - 2.0) / (@n - 1.0) * @variance + x * x / @n
end
end

might be worth trying; Enumerable#max and Enumerable#min do the “right
thing” with nil.

Hope this helps,

Mike

···

Paul J. Sanchez paulNO@SPAMargelfraster.org wrote:

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Hi,

From: “Kurt M. Dresner” kdresner@cs.utexas.edu
Sent: Tuesday, August 12, 2003 11:20 AM

Oh yeah, I’m way out of date.

Depends underlying C library.

soap4r maps “NaN”, “+INF” and “-INF” by itself.
http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/lib/soap4r/lib/soap/XMLSchemaDatatypes.rb?rev=1.57&content-type=text/x-cvsweb-markup

I wish Float to have constans for these special values.
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/62417

Regards,
// NaHi