Ask a Question related to Ruby, Design and Development.
-
Kurt M. Dresner #1
Re: NaN and Inifinity
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 =======<Kurt M. Dresner Guest
-
inifinity recursion && test/unit
Without test/unit ruby finished work and said that stack level too deep. Is this correct that in this code ruby goes to infinity recursion? ruby... -
Scott Thompson #2
Re: NaN and Inifinity
> irb(main):015:0> a = Math::log(-1)
Hrrrm... Okee dokey. I tried something like that and I get> => 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
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
Scott Thompson Guest
-
Harry Ohlsen #3
Re: NaN and Inifinity
Scott Thompson wrote:
Not that wacky. I get the same.> 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. :-)
C:\> ruby -v
ruby 1.8.0 (2003-08-04) [i386-mswin32]
Maybe this has changed recently?
Harry Ohlsen Guest
-
Dan Doel #4
Re: NaN and Inifinity
Scott Thompson wrote:
It's not just you. Happens to me, too, using Ruby 1.8.0pre9 (I believe)>>> 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
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
Dan Doel Guest
-
Mike Stok #5
Re: NaN and Inifinity
In article <B0D86431-CC65-11D7-AFAD-000393803090@mac.com>,
Scott Thompson <easco@mac.com> wrote:I don't understand what you mean by 'assign the variable "a" to NaN'.>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.
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:
=> NaN>> a = 0.0/0.0=> true>> a.nan?=> nil>> a.infinite?=> NaN>> b = 0.0/0.0=> true>> b.nan?=> false>> a == b=> 1>> (+1/0.0).infinite?=> -1>> (-1/0.0).infinite?=> nil>> 1.0.infinite?=> -0.0>> 1 / (-1/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
- [url]http://grouper.ieee.org/groups/754/reading.html[/url] is a good starting
point.
Mike
--
[email]mike@stok.co.uk[/email] | The "`Stok' disclaimers" apply.
[url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
[email]mike@exegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
[url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
Mike Stok Guest
-
Kurt M. Dresner #6
Re: NaN and Inifinity
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 =======<Kurt M. Dresner Guest
-
Kurt M. Dresner #7
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.
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 =======<Kurt M. Dresner Guest
-
NAKAMURA, Hiroshi #8
Re: NaN and Inifinity
Hi,
> From: "Kurt M. Dresner" <kdresner@cs.utexas.edu>
> Sent: Tuesday, August 12, 2003 11:20 AMDepends underlying C library.> Oh yeah, I'm way out of date.
soap4r maps "NaN", "+INF" and "-INF" by itself.
[url]http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/lib/soap4r/lib/soap/XMLSchemaDatatypes.rb?rev=1.57&content-type=text/x-cvsweb-markup[/url]
I wish Float to have constans for these special values.
[url]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/62417[/url]
Regards,
// NaHi
NAKAMURA, Hiroshi Guest
-
Mike Stok #9
Re: NaN and Inifinity
In article <B0D86431-CC65-11D7-AFAD-000393803090@mac.com>,
Scott Thompson <easco@mac.com> wrote:If you are using IEEE 754 math then you can try>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.
[mike@ratdog mike]$ irb --simple-prompt=> -Infinity>> negInf = *['FFF0000000000000'].pack('H*').unpack('G')=> Infinity>> inf = *['7FF0000000000000'].pack('H*').unpack('G')=> -1>> negInf.infinite?=> NaN>> nan = *['7FF8000000000000'].pack('H*').unpack('G')=> true>> nan.nan?=> false>> nan == nan
(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
--
[email]mike@stok.co.uk[/email] | The "`Stok' disclaimers" apply.
[url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
[email]mike@exegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
[url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
Mike Stok Guest
-
Dan North #10
Re: NaN and Inifinity
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
>
>
>
Dan North Guest
-
Paul J. Sanchez #11
Re: NaN and Inifinity
>>>>> "DN" == Dan North <dan@tastapod.com> writes:
DN> Hi Scott. It might help to think of NaN meaning "some
DN> undefined value". Then it makes sense that you can't assign it
DN> - it's undefined - and also that you can't compare two NaNs -
DN> they're both undefined so how can you? All you can do is use
DN> Float.nan? to ask whether the result of what you just did, in
DN> this case log(-1), is undefined. It also helps explain why it
DN> isn't, um, defined in Math or Float.
DN> 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
Paul J. Sanchez Guest



Reply With Quote

