Why is_numeric (NAN) returns TRUE?

I tested the is_numeric function in the NAN constant in PHP and this result

 is_numeric(NAN); // TRUE 

But NAN means "Not A Number". Why is_numeric function returns true ?

I know that NAN is of type float . But when testing the two cases below, the results are different:

 is_float(NAN) // true filter_var(NAN, FILTER_VALIDATE_FLOAT)// false 

Why is there a problem?

Sorry my english is bad

+6
source share
2 answers

NAN is a special constant. It must have some value, so it contains a float data float

 var_dump(NAN); // float(NAN); 

The problem is that filter_var not comparing data types, it is looking for numbers that are not NAN .

Some numerical operations may result in a value represented by a NAN constant. This result represents an undefined or nonrepresentable value in floating point calculations. Any free or strict comparison of this value with any other value, including it, will have a FALSE result.

Because NAN represents any number of different values, NAN should not be compared with other values, including itself, and use is_nan () should be checked instead.

+3
source

NAN is a non-zero floating point value. What is NAN, its square root of (-1) .

So now we are talking about complex numbers. The square root of (-1) is +i or -i , which means the NAN can have two values. Therefore, if the NAN should have been evaluated to -i or false , instead of t2 it will become true.

+1
source

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


All Articles