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或更高版本,否则可能会在不同平台上产生不同的结果.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...