Is there a method to set variable to NaN or Infinity?

A

Alex DeCaria

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
 
J

jbw

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

Colin Bartlett

Is there a built-in method to set a floating point value to NaN or
Infinity? =A0Or, is the only way to do this by
a =3D 0.0/0.0 =A0# =A0set a to NaN
b =3D 3.0/0.0 =A0# =A0set b to Infinity
c =3D -3.0/0.0 =A0# set c to -Infinity

In the past I have done something like:
Float::NaN =3D 0.0 / 0.0 #=3D> NaN
Float::Infinity =3D 3.0 / 1.0 #=3D> Infinity
-Float::Infinity #=3D> -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 Fl=
oat?)
Also, how can I set value to -NaN? =A0This is possible in other languages= ,
but I can't do it in Ruby. =A0I've tried:
a =3D 0.0/0.0
b =3D -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 =3D 0.0 / 0.0 #=3D> NaN
nn =3D -n #=3D> NaN
so I assume that there isn't a "-NaN" in Octave, although as Nan =3D=3D
NaN returns 0 (false)
I'd need to look at the documentation to be sure.

Colin Bartlett
 
A

Alex DeCaria

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
 
M

Mike Stok

=20
In the past I have done something like:
Float::NaN =3D 0.0 / 0.0 #=3D> NaN
Float::Infinity =3D 3.0 / 1.0 #=3D> Infinity

That's a pretty small infinity :)

Mike
-Float::Infinity #=3D> -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?)
=20
=20
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 =3D 0.0 / 0.0 #=3D> NaN
nn =3D -n #=3D> NaN
so I assume that there isn't a "-NaN" in Octave, although as Nan =3D=3D
NaN returns 0 (false)
I'd need to look at the documentation to be sure.
=20
Colin Bartlett
=20

--=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
C

Colin Bartlett

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 =3D 3.0 / 0.0 =A0 #=3D> Infinity
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 Bartlett
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top