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