Re: NaN and Inifinity

Posted: 08-12-2003, 02:55 AM
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 =======<
Reply With Quote

Responses to "Re: NaN and Inifinity"

Scott Thompson
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:06 AM
> 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. :-)

Scott


Reply With Quote
Harry Ohlsen
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:15 AM
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. :-)
Not that wacky. I get the same.

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

Maybe this has changed recently?


Reply With Quote
Dan Doel
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:16 AM
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. :-)
>
> 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


Reply With Quote
Mike Stok
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:20 AM
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.
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
- http://grouper.ieee.org/groups/754/reading.html is a good starting
point.

Mike
--
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
Reply With Quote
Kurt M. Dresner
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:20 AM
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. :-)
> >
> >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 =======<
Reply With Quote
Kurt M. Dresner
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:29 AM
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. :)
>
> Hal
>
>
>======= End of Original Message =======<
Reply With Quote
NAKAMURA, Hiroshi
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:31 AM
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/cvs...-cvsweb-markup

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

Regards,
// NaHi

Reply With Quote
Mike Stok
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-12-2003, 03:38 AM
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.
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

--
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
Reply With Quote
Dan North
Guest
Posts: n/a
 
Re: NaN and Inifinity
Posted: 08-19-2003, 03:33 PM
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
>
>
>


Reply With Quote
 
LinkBack Thread Tools Search this Thread Display Modes
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
inifinity recursion && test/unit Andrey Kulinich Ruby 7 07-02-2003 07:36 PM