Why is "NaN" numerical according to the warning pragma?

I am a bit surprised (and scared) that the warnings pragma does not complain that "NaN" and "nan" are not -numeric.

Why warnings n't warnings highlight the regular 'Argument isn't numeric in addition (+) ?

Test case

 $ 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 
+6
source share
3 answers

From perlop

Binary "<=>" returns -1, 0, or 1, depending on whether the argument on the left is numerically smaller, equal to, or greater than the right argument. If your platform supports NaN (non-digits) as numeric values, using them with "<=>" returns undef. NaN is not "<", "==", ">", "<=" or "> =" nothing (even NaN), so these 5 return false. NaN = = NaN returns true, like NaN! = Something else.

If your platform does not support NaN, then NaN is just a string with a numeric value of 0.

NaN behaves differently on different platforms. It is in a sense numerical, since it can act as such in numerical operations. But this is also really not a number, because it has the value undefined.

In addition, his behavior is not tolerated as:

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

may produce different results on different platforms if you are not using Perl 5.22 or later.

+10
source

"Not a number" is the long name for the value, which is most often represented as "NaN". See Also Wikipedia article . Computing with NaN actually makes sense (it even has an actual representation of the bit level in IEEE754).

+7
source

NaN is a numeric copy of NiL in strings.

-4
source

Source: https://habr.com/ru/post/919763/


All Articles