On Thu, Apr 1, 2010 at 3:57 PM, Alex DeCaria [email protected] 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.
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.
Float::Infinity = 3.0 / 1.0 #=> Infinity
That’s a pretty small infinity
Mike
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.
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
Well, us wot have studied maffematics can deal with LOADS of infinitys!
Or, in other words, oops!
Yes that should have been
Float::Infinity = 3.0 / 0.0 #=> Infinity
Alex DeCaria to ruby-talk wrote:
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.
Fair enough!
As far as I know, Ruby uses “standard” floating point (so no negative
or “signed” NaN?), but I think we now need an answer from an expert.
Colin B.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.