Reputation: 3542
I have tested the is_numeric
function in the NAN
constant in PHP and the given result
is_numeric(NAN); // TRUE
But NAN means "Not A Number". Why the function is_numeric
returns true
?
I know that NAN is of float
type. But when testing the two cases below, the results are different:
is_float(NAN) // true
filter_var(NAN, FILTER_VALIDATE_FLOAT)// false
Why ocurred it?
Sorry, my english is bad
Upvotes: 9
Views: 1049
Reputation: 12937
NAN is a non-zero floating point value. What is NAN, Its a square root of (-1)
.
So now it comes to complex numbers. Square root of (-1)
is +i
or -i
which means NAN can have two values. So if NAN
were to evaluate to -i
or false
, NAN == false
would become true instead.
Upvotes: 1
Reputation: 31614
NAN
is a special constant. It has to hold some value, so it holds a float
datatype
var_dump(NAN); // float(NAN);
The problem is that filter_var
isn't comparing data types, it's looking for numbers, which NAN
is not.
Some numeric operations can result in a value represented by the constant NAN. This result represents an undefined or unrepresentable value in floating-point calculations. Any loose or strict comparisons of this value against any other value, including itself, will have a result of FALSE.
Because NAN represents any number of different values, NAN should not be compared to other values, including itself, and instead should be checked for using is_nan().
Upvotes: 4