In one of my modules, I have to deal with the concept of infinity. To date, I have been using 9**9**9
as positive infinity, and this seems to work well, is fast, and seems to be what perl's internals use as infinity.
However, things get a bit dicey if a user of my module decides to use one of the big number modules (like use bigint;
), and then they use inf
or Math::BigInt->binf()
to represent infinity.
In some places it seems to work fine, but in others, comparisons that should be true or should be false end up the wrong way round leading to difficult to track down bugs.
I would like to support the various other notions of infinity with something that will work with both normal perl numbers, and arbitrary precision numbers.
But I also have concerns about performance since some of my comparisons to infinity occur in tight inner loops. Obviously inf
from Math::BigInt
is going to be slower than 9**9**9
(due to either calling tied or overloaded methods on each access). Has anyone dealt with this problem in the past? If so, what was your solution?
I've thought about using my own constant for infinity, defined something like this:
use constant INF => if_any_bignum_modules_loaded()
? Math::BigInt->binf
: 9**9**9;
And then adding the caveat to my module that any bignum modules should be loaded first. Does this sound sensible? Is there a reliable implementation of if_any_bignum...
out there, or should I roll my own?