http://qs321.pair.com?node_id=783071

jdporter has asked for the wisdom of the Perl Monks concerning the following question: (math)

N/T

Originally posted as a Categorized Question.

• Comment on How do I create/detect/handle Infinity?

Replies are listed 'Best First'.
Re: How do I create/detect/handle Infinity?
by shmem (Chancellor) on Jul 25, 2009 at 11:04 UTC

Creating Inf/NaN:

```use Devel::Peek;
\$inf = (~@~)**(~@~);
\$nan = \$inf / \$inf;
Dump \$_ for \$inf, \$nan;
__END__
SV = NV(0x9e15f84) at 0x9dfc11c
REFCNT = 2
FLAGS = (NOK,pNOK)
NV = inf
SV = NV(0x9e15f9c) at 0x9dfc59c
REFCNT = 2
FLAGS = (NOK,pNOK)
NV = nan

Detecting Inf/NaN: since they are numeric values, Inf and NaN can be tested for with the numeric equality/inequality operators == and !=. For Inf, greater and less also work.

Handling: not useful ;-)

Devel::Peek

NaN doesn't test as == anything, even another NaN.
NaN doesn't test as == anything, even another NaN.

I was wondering:
```unless (\$x <= \$inf) { print "\\$x is definitely a NaN\n" }
else { print "\\$x is definitely NOT a NaN\n" }
Is that reliable ? ... assuming, of course, that \$inf is in fact + infinity, and that there are no bugs present.
Seems to me that ought to be good enough.

Cheers,
Rob
Re: How do I create/detect/handle Infinity?
by moritz (Cardinal) on Jul 24, 2009 at 18:21 UTC
I think in Perl 5 this is platform specific, so I'll rather give the Perl 6 answer:
```my \$num = Inf;
given \$num {
say "+Inf" when Inf;
say "-Inf" when -Inf;
say "NaN"  when NaN;
}