From: Alex DeCaria on
Is there a built-in method to set a floating point value to NaN or
Infinity? Or, is the only way to do this by

a = 0.0/0.0 # set a to NaN

b = 3.0/0.0 # set b to Infinity

c = -3.0/0.0 # set c to -Infinity

Also, how can I set value to -NaN? This is possible in other languages,
but I can't do it in Ruby. I've tried:

a = 0.0/0.0

b = -a

but b still comes out NaN.

--Alex
--
Posted via http://www.ruby-forum.com/.

From: jbw on
[Note: parts of this message were removed to make it a legal post.]

I read in the BigDecimal docs you can do n = BigDecimal.new('NaN'). But
i'm unfamiliar with it


On Thu, Apr 1, 2010 at 3:57 PM, Alex DeCaria
<alex.decaria(a)millersville.edu>wrote:

> Is there a built-in method to set a floating point value to NaN or
> Infinity? Or, is the only way to do this by
>
> a = 0.0/0.0 # set a to NaN
>
> b = 3.0/0.0 # set b to Infinity
>
> c = -3.0/0.0 # set c to -Infinity
>
> Also, how can I set value to -NaN? This is possible in other languages,
> but I can't do it in Ruby. I've tried:
>
> a = 0.0/0.0
>
> b = -a
>
> but b still comes out NaN.
>
> --Alex
> --
> Posted via http://www.ruby-forum.com/.
>
>


--
jbw

From: Colin Bartlett on
On Thu, Apr 1, 2010 at 3:57 PM, Alex DeCaria
<alex.decaria(a)millersville.edu> wrote:
> Is there a built-in method to set a floating point value to NaN or
> Infinity?  Or, is the only way to do this by
> a = 0.0/0.0  #  set a to NaN
> b = 3.0/0.0  #  set b to Infinity
> c = -3.0/0.0  # set c to -Infinity

In the past I have done something like:
Float::NaN = 0.0 / 0.0 #=> NaN
Float::Infinity = 3.0 / 1.0 #=> Infinity
-Float::Infinity #=> -Infinity
In other words, do they have to be methods? Can they just be constants?
(But I think it might be useful to have those constants automatically in Float?)

> Also, how can I set value to -NaN?  This is possible in other languages,
> but I can't do it in Ruby.  I've tried:
> a = 0.0/0.0
> b = -a
> but b still comes out NaN.

I'm curious: which languages have a -NaN value and why would you want
to use it instead of just NaN?
I've just tried Octave, and in that
n = 0.0 / 0.0 #=> NaN
nn = -n #=> NaN
so I assume that there isn't a "-NaN" in Octave, although as Nan ==
NaN returns 0 (false)
I'd need to look at the documentation to be sure.

Colin Bartlett

From: Alex DeCaria on
> I'm curious: which languages have a -NaN value and why would you want
> to use it instead of just NaN?

I do a lot of my scientific programming in IDL. It allows explicit
assignment of NaN and -NaN to floating point variables, as well as
returns -NaN under certain circumstances. IDL is used for a lot of
image processing, and being able to have NaN and/or -NaN is useful as a
mask when processing the arrays.

--Alex
--
Posted via http://www.ruby-forum.com/.

From: Mike Stok on

On Apr 1, 2010, at 3:48 PM, Colin Bartlett wrote:

> On Thu, Apr 1, 2010 at 3:57 PM, Alex DeCaria
> <alex.decaria(a)millersville.edu> wrote:
>> Is there a built-in method to set a floating point value to NaN or
>> Infinity? Or, is the only way to do this by
>> a = 0.0/0.0 # set a to NaN
>> b = 3.0/0.0 # set b to Infinity
>> c = -3.0/0.0 # set c to -Infinity
>
> In the past I have done something like:
> Float::NaN = 0.0 / 0.0 #=> NaN
> Float::Infinity = 3.0 / 1.0 #=> Infinity

That's a pretty small infinity :-)

Mike

> -Float::Infinity #=> -Infinity
> In other words, do they have to be methods? Can they just be constants?
> (But I think it might be useful to have those constants automatically in Float?)
>
>> Also, how can I set value to -NaN? This is possible in other languages,
>> but I can't do it in Ruby. I've tried:
>> a = 0.0/0.0
>> b = -a
>> but b still comes out NaN.
>
> I'm curious: which languages have a -NaN value and why would you want
> to use it instead of just NaN?
> I've just tried Octave, and in that
> n = 0.0 / 0.0 #=> NaN
> nn = -n #=> NaN
> so I assume that there isn't a "-NaN" in Octave, although as Nan ==
> NaN returns 0 (false)
> I'd need to look at the documentation to be sure.
>
> Colin Bartlett
>

--

Mike Stok <mike(a)stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.