Skip to content

Core\Math::isNan

Core\Math::isNan(float $n): boolCore\Math::isNan(
    float $n,
): bool

Core\Math::isNan returns true when a float is NaN. NaN means "not a number". It is the result of a calculation that has no answer, such as zero divided by zero with Core\Math::divide, or the square root of a negative number.

You cannot find NaN with ==. NaN is not equal to any value, and it is not equal to itself either. So $x == Core\Math::NAN is always false. Use Core\Math::isNan instead.

INFINITY and -INFINITY are not NaN, so Core\Math::isNan returns false for them. Core\Math::isFinite returns false for all three.

The examples below show which calculations give NaN, then why == cannot find it, then a program that skips the failed readings from a sensor before it works out the average.

Parameters

  • $nfloatThe value to test.

Return value

Returns bool. true when $n is NaN. false for every other float, INFINITY and -INFINITY included.

Examples

Which values are nan

<?nvs
// Shows which calculations give `NaN` and which do not.
class Check
{
// Prints the label, then "NaN" or "not NaN".
public static function show(string $label, float $n): void
{
echo $label, ': ', Core\Math::isNan($n) ? 'NaN' : 'not NaN', "\n";
}
}
// Zero divided by zero has no answer. This prints "0 / 0: NaN".
Check::show('0 / 0', Core\Math::divide(0.0, 0.0));
// A negative number has no square root. This prints "sqrt(-1): NaN".
Check::show('sqrt(-1)', Core\Math::sqrt(-1.0));
// One divided by zero is `INFINITY`, and `INFINITY` is not `NaN`.
// This prints "1 / 0: not NaN".
Check::show('1 / 0', Core\Math::divide(1.0, 0.0));
// An ordinary number is not `NaN`. This prints "2.5: not NaN".
Check::show('2.5', 2.5);
Output
0 / 0: NaN
sqrt(-1): NaN
1 / 0: not NaN
2.5: not NaN

Equals cannot find nan

<?nvs
// Shows why `==` cannot find `NaN`, and how `Core\Math::isNan` finds it.
float $result = Core\Math::sqrt(-4.0);
// `NaN` is not equal to any value. It is not equal to itself either.
// This prints "with ==: different".
echo 'with ==: ', $result == $result ? 'equal' : 'different', "\n";
// This also prints "with ==: different". A test like this never finds `NaN`.
echo 'with ==: ', $result == Core\Math::NAN ? 'equal' : 'different', "\n";
// `Core\Math::isNan` tests the value itself. This prints "with isNan: NaN".
echo 'with isNan: ', Core\Math::isNan($result) ? 'NaN' : 'a number', "\n";
Output
with ==: different
with ==: different
with isNan: NaN

Skip failed readings

<?nvs
// Shows how a program skips the failed readings from a sensor before it works out the average.
// The sensor stores `NaN` when a reading fails.
array<float> $readings = [21.5, Core\Math::NAN, 22.0, 22.5, Core\Math::NAN];
float $sum = 0.0;
float $count = 0.0;
foreach ($readings as float $reading) {
// One `NaN` in the sum makes the whole sum `NaN`, so this loop skips it.
if (Core\Math::isNan($reading)) {
continue;
}
$sum = $sum + $reading;
$count = $count + 1.0;
}
// Three readings are left, and their average is 22. This prints "3 readings, average 22".
echo $count, ' readings, average ', $sum / $count, "\n";
Output
3 readings, average 22