加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:57:58 所属栏目:大数据 来源:网络整理
导读:我对 warnings pragma不抱怨’NaN’和’nan’不是数字这一事实感到有点惊讶(并且害怕). 为什么警告不会发出习惯性的’参数对于他们来说不是数字()? 测试用例 $perl -Mstrict -wE 'say 0+$_ for qw/string NaN nan fail/;'Argument "string" isn't numeric i
我对 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或更高版本,否则可能会在不同平台上产生不同的结果.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读