I am currently working on writing a small math related library in php. I know there exists some kind of an analogue of javascript's NaN
in php, it is a constant NAN
of float
type.
So far I have not ever come across this constant apart from php standard functions like asin()
and alike, which will return this NAN
value if you pass them an invalid parameter (>1 in this exact case).
This value is not detected by is_float()
and is_numeric()
since it is of float
type, both of these functions return true
and the only way to check it is is_nan()
. Also, casting it to (int)
will result in 0
;
$x = asin(3); // $x = NAN
is_float($x); // true
is_numeric($x); // true
$d = (int)$x // $d = 0;
is_nan(0.5) // false
is_nan($x) // true
But none of existing libs whereof I know uses this behaviour, instead throwing an error is a common choice, as it seems.
So my questions are: which approach ought I to choose? why nowhere, apart from standard php functions, the NAN
is used?