[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Infinity

hadley wickham

7/22/2006 7:23:00 PM

irb(main):084:0> (-1.0/0.0)
=> -Infinity
irb(main):086:0> (1.0/0.0)
=> Infinity

but

irb(main):087:0> Infinity
NameError: uninitialized constant Infinity
from (irb):87
from :0

is it possible to directly access/create a float representing positive
or negative infinity?

Thanks,

Hadley

13 Answers

Marcel Molina Jr.

7/22/2006 7:25:00 PM

0

On Sun, Jul 23, 2006 at 04:22:37AM +0900, hadley wickham wrote:
> irb(main):084:0> (-1.0/0.0)
> => -Infinity
> irb(main):086:0> (1.0/0.0)
> => Infinity
>
> but
>
> irb(main):087:0> Infinity
> NameError: uninitialized constant Infinity
> from (irb):87
> from :0
>
> is it possible to directly access/create a float representing positive
> or negative infinity?

Infinity = 1.0/0

(N.B. Doesn't work on all platforms.)

marcel
--
Marcel Molina Jr. <marcel@vernix.org>

hadley wickham

7/22/2006 7:38:00 PM

0

> Infinity = 1.0/0
>
> (N.B. Doesn't work on all platforms.)

But it will everywhere that supports the IEEE floating point standard?

Thanks,

Hadley

Timothy Goddard

7/22/2006 10:57:00 PM

0

Others have answered the main question here but it's worth noting for
the future that just because it says infinity doesn't mean a constant
exists with that name. Check this out:

irb(main):001:0> class A; end
=> nil
irb(main):002:0> class B; end
=> nil
irb(main):003:0> A, B = B, A
(irb):3: warning: already initialized constant A
(irb):3: warning: already initialized constant B
=> [B, A]
irb(main):004:0> A
=> B
irb(main):005:0> B
=> A

The name is independent of constants.

hadley wickham wrote:
> irb(main):084:0> (-1.0/0.0)
> => -Infinity
> irb(main):086:0> (1.0/0.0)
> => Infinity
>
> but
>
> irb(main):087:0> Infinity
> NameError: uninitialized constant Infinity
> from (irb):87
> from :0
>
> is it possible to directly access/create a float representing positive
> or negative infinity?
>
> Thanks,
>
> Hadley

Caleb Clausen

7/22/2006 11:41:00 PM

0

On 7/22/06, Marcel Molina Jr. <marcel@vernix.org> wrote:
> Infinity = 1.0/0
>
> (N.B. Doesn't work on all platforms.)

Does anyone know a definition for Infinity that does work on all
platforms? Here's what I use; I have no idea how robust it really is:

Infinity= begin
result= [ Float::MAX**Float::MAX, Float::MAX**2, Float::MAX*2].max
result.infinite? ? result : result=1.0/0
rescue: Float::MAX #maybe 1.0/0 doesn't work on some systems?
end unless defined? Infinity

#there's also this way:
999999999999999999999999999999999999999999999999e99999999999999999
#but stuff like that sometimes returns zero, so it doesn't seem as reliable.
#(plus it generates a warning)

hadley wickham

7/23/2006 7:39:00 AM

0

> Others have answered the main question here but it's worth noting for
> the future that just because it says infinity doesn't mean a constant
> exists with that name. Check this out:

Yes, but a lot of languages provide some way of accessing the
"constants" that represent special floating point numbers: Inf, -Inf
and NaN

Hadley

M. Edward (Ed) Borasky

7/23/2006 9:03:00 AM

0

hadley wickham wrote:
>> Others have answered the main question here but it's worth noting for
>> the future that just because it says infinity doesn't mean a constant
>> exists with that name. Check this out:
>
> Yes, but a lot of languages provide some way of accessing the
> "constants" that represent special floating point numbers: Inf, -Inf
> and NaN
I think Matz vetoed anything that required IEEE floating point a little
while ago. So ... for Ruby code to use Inf, -Inf or NaN, there would
need to be special care taken to maintain portability.


hadley wickham

7/23/2006 10:41:00 AM

0

> > Yes, but a lot of languages provide some way of accessing the
> > "constants" that represent special floating point numbers: Inf, -Inf
> > and NaN
> I think Matz vetoed anything that required IEEE floating point a little
> while ago. So ... for Ruby code to use Inf, -Inf or NaN, there would
> need to be special care taken to maintain portability.

Thanks for that explanation, although it does worry me a little, I
understand that getting floating point working precisely correctly
across multiple platforms is very hard.

Regards,
Hadley

Trans

7/23/2006 12:04:00 PM

0

Facets has Infinity (actaully it was deprecated for a while until I
fixed. Your post inspired me to fix it, so it's now back in the very
latest version. Thanks!)

It uses the constant INFINITY. Is that cool? Or is there some sort of
general practice of using Inf?

Thanks,
T.

hadley wickham

7/23/2006 12:34:00 PM

0

> Facets has Infinity (actaully it was deprecated for a while until I
> fixed. Your post inspired me to fix it, so it's now back in the very
> latest version. Thanks!)

That's great! Thanks.

> It uses the constant INFINITY. Is that cool? Or is there some sort of
> general practice of using Inf?

I think Inf is more common: R uses it, so does MATLAB
(http://www.mathworks.com/access/helpdesk/help/techdoc/re...),
and there's a PEP for python that uses it
(http://www.python.org/dev/peps..., which also has a nice
discussion of these issues)

Hadley

Trans

7/23/2006 3:07:00 PM

0


hadley wickham wrote:
> > Facets has Infinity (actaully it was deprecated for a while until I
> > fixed. Your post inspired me to fix it, so it's now back in the very
> > latest version. Thanks!)
>
> That's great! Thanks.
>
> > It uses the constant INFINITY. Is that cool? Or is there some sort of
> > general practice of using Inf?
>
> I think Inf is more common: R uses it, so does MATLAB
> (http://www.mathworks.com/access/helpdesk/help/techdoc/re...),
> and there's a PEP for python that uses it
> (http://www.python.org/dev/peps..., which also has a nice
> discussion of these issues)

Nice, that was helpful. I added some query methods to Numeric, eg.
finite?, infinite?, ... and did this:

UNDEFINED = InfinityClass.instance(0)
INFINITY = InfinityClass.instance(1)

NaN = UNDEFINED
Inf = INFINITY
PosInf = INFINITY
NegInf = -INFINITY

Thanks!
T.