P is for Practical PerlMonks

### Re: Answer: How do I create/detect/handle Infinity?

by ysth (Canon)
 on Jul 26, 2009 at 06:26 UTC ( #783273=note: print w/replies, xml ) Need Help??

NaN doesn't test as == anything, even another NaN.
• Comment on Re: Answer: How do I create/detect/handle Infinity?

Replies are listed 'Best First'.
Re^2: Answer: How do I create/detect/handle Infinity?
by syphilis (Chancellor) on Jul 26, 2009 at 08:41 UTC
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
Yes. Other ways:
```\$isnan = (\$x != \$x);
\$isnan = ! defined(\$x <=> 0);
\$isnan = ! defined(\$x <=> 0);

Ooow ... wasn't aware that perl did that.
In the mpfr library, the mpfr_cmp(x,y) function will return -1 if x<y, +1 if x>y. Otherwise it returns 0 (and sets the erange flag iff one/both of the arguments is a nan).

So, I'm left wondering whether, in Math::MPFR (which more-or-less wraps the mpfr library), the spaceship operator (which overloads the mpfr_cmp function) should:
a) adhere to mpfr library behaviour and return 0 and set the erange flag when nans are involved;
or
b) adhere to perl behaviour and return undef when nans are involved.

If b), then there's also the question of whether the erange flag should still be set.

Interested to hear any thoughts on that.

Cheers,
Rob

Create A New User
Node Status?
node history
Node Type: note [id://783273]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2017-06-22 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (525 votes). Check out past polls.