微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

perl – 为什么’NaN’数字根据警告pragma?

我对 warnings pragma不抱怨’NaN’和’nan’不是数字这一事实感到有点惊讶(并且害怕).

为什么警告不会发出习惯性的’参数对于他们来说不是数字()?

测试用例

$perl -Mstrict -wE 'say 0+$_ for qw/string NaN nan fail/;'
Argument "string" isn't numeric in addition (+) at -e line 1.
0
0
0
Argument "fail" isn't numeric in addition (+) at -e line 1.
0

解决方法

perlop

Binary “<=>” returns -1,or 1 depending on whether the left
argument is numerically less than,equal to,or greater than the right
argument. If your platform supports NaNs (not-a-numbers) as numeric
values,using them with “<=>” returns undef. NaN is not “<“,“==”,
“>”,“<=” or “>=” anything (even NaN),so those 5 return false. NaN !=
NaN returns true,as does NaN != anything else.

If your platform
doesn’t support NaNs then NaN is just a string with numeric value 0.

NaN在不同平台上表现不同.它在某种程度上是数值的,因为它可以在数值运算中起作用.但它也不是一个数字,因为它具有未定义的价值.

此外,它的行为不可移植:

perl -E "say 'yes' if 0 == 'NaN'"

除非您使用Perl 5.22或更高版本,否则可能会在不同平台上产生不同的结果.

原文地址:https://www.jb51.cc/Perl/171753.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐