PHP: is_nan - Manual (original) (raw)
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
is_nan — Checks whether a float is NAN
Description
[NAN](math.constants.php#constant.nan) is returned from mathematical operations that are undefined, for example when passing parameters outside of function’s input domain. The square root (sqrt()) is only defined for positive numbers, passing a negative number will result in [NAN](math.constants.php#constant.nan). Other examples of operations returning [NAN](math.constants.php#constant.nan) are dividing [INF](math.constants.php#constant.inf) by [INF](math.constants.php#constant.inf) and any operation involving an existing [NAN](math.constants.php#constant.nan) value.
Note:
Despite its name of Not A Number,
[NAN](math.constants.php#constant.nan)is a valid value of type float.
Caution
[NAN](math.constants.php#constant.nan) does not compare equal to [NAN](math.constants.php#constant.nan). To check whether a float is [NAN](math.constants.php#constant.nan), is_nan() must be used. Checking$float === NAN will not work.
Parameters
num
The float to check
Examples
Example #1 is_nan() example
<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mi>a</mi><mi>n</mi><mo>=</mo><mi>s</mi><mi>q</mi><mi>r</mi><mi>t</mi><mo stretchy="false">(</mo><mo>−</mo><mn>1</mn><mo stretchy="false">)</mo><mo separator="true">;</mo><mi>v</mi><mi>a</mi><msub><mi>r</mi><mi>d</mi></msub><mi>u</mi><mi>m</mi><mi>p</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">nan = sqrt(-1);var_dump(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">nan</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal" style="margin-right:0.03588em;">q</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">t</span><span class="mopen">(</span><span class="mord">−</span><span class="mord">1</span><span class="mclose">)</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">a</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:-0.0278em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">d</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal">m</span><span class="mord mathnormal">p</span><span class="mopen">(</span></span></span></span>nan, is_nan($nan)); ?>
The above example will output:
See Also
- is_finite() - Checks whether a float is finite
- is_infinite() - Checks whether a float is infinite
Found A Problem?
darkangel at moveinmod dot net ¶
19 years ago
nan/"not a number" is not meant to see if the data type is numeric/textual/etc..
NaN is actually a set of values which can be stored in floating-point variables, but dont actually evaluate to a proper floating point number.
The floating point system has three sections: 1 bit for the sign (+/-), an 8 bit exponent, and a 23 bit fractional part.
There are rules governing which combinations of values can be placed into each section, and some values are reserved for numbers such as infinity. This leads to certain combinations being invalid, or in other words, not a number.
10 years ago
I would use is_numeric() instead of ctype_digit() if you cannot be 100% sure what data type the string will be. Example from the docs:
<?php
$numeric_string = '42';
$integer = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer); // false (ASCII 42 is the * character)
is_numeric($numeric_string); // true
is_numeric($integer); // true
?>
5 years ago
function is_nan2($n) {
return <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo stretchy="false">!</mo><mo>=</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">n !== </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">n</span><span class="mclose">!</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">==</span></span></span></span>n;
}
is_nan2(NAN); // true
9 years ago
Starting with PHP 7, the string 'NaN' evaluates to the NaN value as well.
Example:
var_dump( (float) 'NaN' );
PHP 5.x and HHVM:
float(0)
PHP 7.0:
float(NAN)