Perl 中是否有 inf 常量? [英] Is there an inf constant in Perl?
问题描述
我正在为算法初始化一个具有无穷大的列表.编写 $x = 9**9**9
感觉很不直观,而且,我将来可能想使用 BigInt.1/0
抛出错误.
I'm initialising a list with infinities for an algorithm. Writing $x = 9**9**9
feels unintuitive, and also, I might want to use BigInt in the future. 1/0
throws an error.
获得 inf
的规范方法是什么?
What's the canonical way to get inf
?
推荐答案
您可以使用特殊字符串 "inf"
:
You can use the special string "inf"
:
perl -E'say "inf" + 1'
inf
perl -E'say 1 / "inf"'
0
等等.
其他特殊字符串包括+inf
、-inf
、nan
.当然,这也适用于 bignum
或 bigint
编译指示.但是,这些编译指示导出等效函数 inf
和 NaN
,以便您可以使用裸字.
Other special strings include +inf
, -inf
, nan
. Of course this also works with bignum
or bigint
pragmas. However, these pragmas export equivalent functions inf
and NaN
so that you can use barewords.
正如@ikegami 指出的那样,似乎没有一种便携 的方法可以在没有模块的情况下实现真正的无穷大.我刚刚浏览了这个有趣的 perlmonks 线程,但它并没有减少混乱.也许最好的解决方案是接受性能损失并从一开始 使用 big{num,int,rat}
,但使用 no big{num,int,rat}
在不需要它们的范围内.
As @ikegami pointed out, there doesn't seem to be a portable way of accomplishing true infinities without a module. I just waded through this interesting perlmonks thread, but it doesn't get less confusing. Perhaps the best solution would be to accept the performance penalty and use big{num,int,rat}
from the start, but use no big{num,int,rat}
in scopes where they aren't required.
这篇关于Perl 中是否有 inf 常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!