Perl: the Markov chain saw PerlMonks

### Re: \$array[ 'Infinity' ]

by Sixtease (Friar)
 on Dec 16, 2007 at 23:28 UTC ( #657342=note: print w/replies, xml ) Need Help??

in reply to \$array[ 'Infinity' ]

No, this is not caused by numerical equivalence to zero. Nor is it caused by Infinity/inf differences:
```@a = (5 .. 10);
print
\$a['inf'], "\n",
\$a[-inf], "\n",
-inf==0 ? "-inf is zero\n" : "-inf isn't zero\n",
'inf'==0 ? "inf is zero\n" : "inf isn't zero\n";

# still outputs 10 and 5 and states that the infinities aren't zeroes

perl --version says "This is perl, v5.8.8 built for x86_64-linux-thread-multi"

Update: The weird thing is that indexing by -inf gives the first element and indexing by inf gives the last one. That's not what I'd expect.

use strict; use warnings; print "Just Another Perl Hacker\n";

Replies are listed 'Best First'.
Re^2: \$array[ 'Infinity' ]
by quester (Vicar) on Dec 17, 2007 at 00:25 UTC
Using perl -MO=Deparse may shed some light on some of this weirdness, although I have no idea why on Earth anyone would implement 'inf' the way it works on my box.
```perl -MO=Deparse -e 'print \$n[inf]'
prints
```print \$n[9**9**9];
as does
```perl -MO=Deparse -e 'print \$n["inf"]'

On the other hand,

```perl -MO=Deparse -e 'print \$n[-inf]'
prints
```print \$n[-'inf'];

```perl -MO=Deparse -e 'print "", inf==0 ? "a" : "b", 'inf'==0 ? "c" : "d
+", -inf==0 ? "e" : "f"'
prints
```print '', 'inf' == 0 ? 'a' : 'b', 'inf' == 0 ? 'c' : 'd', -'inf' == 0
+? 'e' : 'f';

As nearly as I can tell at the moment, inf is only equivalent 9**9**9 if it is used by itself as a subscript; it is taken as a bareword in a simple assignment, so

```perl -MO=Deparse -e '\$n[inf], \$n[1+inf], \$n[-inf] = inf'
prints
```\$n[9**9**9], \$n[1 + 'inf'], \$n[-'inf'] = 'inf';

My perl -v shows,

```This is perl, v5.8.8 built for i486-linux-gnu-thread-multi

Does anyone know why? - quester

That is terrifyingly weird. But the 9**9**9 is just an artifact of B::Deparse, as can be seen here. I'm not sure why it does that.

Then again, it makes a kind of twisted sense that \$a[inf] means "last element of @a", and (completely by accident) \$a[-inf] means "last element counting from the end."

Oddly (or maybe not so oddly) I get different output on a w2k box from the first cited by quester:
```perl -MO=Deparse -e "print \$n[inf]"
print \$n[0];
-e syntax OK
from...
```perl -v

This is perl, v5.8.8 built for MSWin32-x86-multi-thread
(with 33 registered patches, see perl -V for more detail)
...
Binary build 819 [267479] provided by ActiveState http://www.ActiveSta
+te.com
Built Aug 29 2006 12:42:41
It's been established that ActiveState's build doesn't support infinity.

As to the "why", X = 9**9**9 == 9**(9**9) = 9**387420489. Nine to that power is a number aproximately 1.2 billion bits long. (log(9)/log(2) * (9**9)). So, for all intents and purposes, it's infinity. Since it's odd, -X will be negative infinity So, it avoids the (possible?) non-portability of a raw 'inf' or '-inf'.

Update: Duh, Ben. Re: strikeout. Plus it's specifically negated. I was thinking (-9)**9**9)

Re^2: \$array[ 'Infinity' ]
by almut (Canon) on Dec 16, 2007 at 23:52 UTC

Interesting.  I can confirm your results for "v5.8.8 built for x86_64-linux-thread-multi".  For "v5.8.8 built for i486-linux-gnu-thread-multi", however, I'm just getting 10 for 'Infinity' (or inf) and nothing (undef) for -Infinity (or -inf).

Create A New User
Node Status?
node history
Node Type: note [id://657342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2020-11-23 16:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?