如何在 Perl 中创建或测试 NaN 或无穷大?
如何在 Perl 中创建或测试 NaN 或无限值?
How do I create or test for NaN or infinite values in Perl?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 Perl 中创建或测试 NaN 或无限值?
How do I create or test for NaN or infinite values in Perl?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
编辑:修复负无穷大。
EDIT: Fixed for negative infinity.
这是一个相当可靠的方法:
输出是:
Here's a fairly reliable way:
Output is:
使用 CPAN 中的 Data::Float。 它导出以下函数:
float_is_infinite(
)float_is_nan()
与此处发布的其他半工作解决方案相反,它有一个 测试套件。
Use Data::Float from CPAN. It exports the following functions:
float_is_infinite(
)float_is_nan()
And contrary to the others half-working solutions posted here, it has a testsuite.
就我个人而言,我会使用 Math::BigFloat(或 BigInt)来处理任何接近无穷大的 NaN 的情况。
既然已经有模块可以完成这项工作,为什么还要用黑客解决方案重新发明轮子呢?
Personally, I would use
Math::BigFloat
(orBigInt
) for anything that is going to touch infinity ofNaN
.Why reinvent the wheel with a hack solution when there are already modules that do the job?
当我搜索时,我得到了这个网站(此处)和 http ://www.learning-perl.com/2015/05/perls-special-not-a-numbers/
链接的文章指出,当底层时,“nan”==“nan”永远不会成立c 实现支持 NaN,因为 Nan 无法匹配自身。
这很好地说明了这一点,
我猜想在 Perl 已正常降级且代码尚未降级的环境中运行代码的风险可能足够低,因此您不必太担心。
当然,如果您不希望 Perl 作为数字进行插值,请使用 'eq' 而不是 '=='
When I searched I got this site (here) and http://www.learning-perl.com/2015/05/perls-special-not-a-numbers/
The linked article points out that "nan" == "nan" is never true, when the underlying c implementation supports NaN because Nan cannot match itself.
This is nicely illustrated with
I guess the risk of running you code in an environment where perl has degraded gracefully and your code has not might be low enough so that you don't worry too much.
And of course if you don't want perl to interpolate as a number, use 'eq' not '=='
NaN 在输入上是
NaN
(但在输出上不是;那里是nan
(无论出于何种原因))。Perl 可以理解
inf
和-inf
:NaN is
NaN
on input (but not on output; there it'snan
(for whatever reason)).And Perl understands
inf
and-inf
:下面是有效的简洁答案。
1: 如何创建用于输出的“NAN”变量(例如 printf):
2: 如何测试“NAN”(看起来像 ascii 艺术):
3: 如何创建“INF”和 INFN 变量:
4 :如何测试“INF”(任何符号):
Succinct answer that works follows.
1: How to create a "NAN" variable for output (to printf, for example):
2: How to test for "NAN" (looks like ascii art):
3: How to create an "INF" and INFN variables:
4: How to test for "INF" (of any sign):