Professional Web Applications Themes

NaN and Inifinity - Ruby

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 > ...

  1. #1

    Default 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

  2. #2

    Default Re: NaN and Inifinity

    > 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


    Scott Thompson Guest

  3. #3

    Default Re: NaN and Inifinity

    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?


    Harry Ohlsen Guest

  4. #4

    Default Re: NaN and Inifinity

    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


    Dan Doel Guest

  5. #5

    Default Re: NaN and Inifinity

    In article <B0D86431-CC65-11D7-AFAD-000393803090mac.com>,
    Scott Thompson <eascomac.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
    - [url]http://grouper.ieee.org/groups/754/reading.html[/url] is a good starting
    point.

    Mike
    --
    [email]mikestok.co.uk[/email] | The "`Stok' disclaimers" apply.
    [url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
    [email]mikeexegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
    [url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
    Mike Stok Guest

  6. #6

    Default 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

  7. #7

    Default 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" <mikeratdog.stok.co.uk>
    > Newsgroups: comp.lang.ruby
    > To: "ruby-talk ML" <ruby-talkruby-lang.org>
    > Sent: Monday, August 11, 2003 9:21 PM
    > Subject: Re: NaN and Inifinity
    >
    >
    > > In article <B0D86431-CC65-11D7-AFAD-000393803090mac.com>,
    > > Scott Thompson <eascomac.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

  8. #8

    Default Re: NaN and Inifinity

    Hi,
    > From: "Kurt M. Dresner" <kdresnercs.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.
    [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

  9. #9

    Default Re: NaN and Inifinity

    In article <B0D86431-CC65-11D7-AFAD-000393803090mac.com>,
    Scott Thompson <eascomac.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

    [mikeratdog 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

    --
    [email]mikestok.co.uk[/email] | The "`Stok' disclaimers" apply.
    [url]http://www.stok.co.uk/~mike/[/url] | GPG PGP Key 1024D/059913DA
    [email]mikeexegenix.com[/email] | Fingerprint 0570 71CD 6790 7C28 3D60
    [url]http://www.exegenix.com/[/url] | 75D2 9EC4 C1C0 0599 13DA
    Mike Stok Guest

  10. #10

    Default 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

  11. #11

    Default Re: NaN and Inifinity

    >>>>> "DN" == Dan North <dantastapod.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

Similar Threads

  1. inifinity recursion && test/unit
    By Andrey Kulinich in forum Ruby
    Replies: 7
    Last Post: July 2nd, 06:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139