Skip to content

Core\Math::isFinite

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

Core\Math::isFinite returns true when a float is an ordinary number. A float can also be one of three special values: INFINITY, -INFINITY and NaN (a value that means "not a number"). For those three, the result is false.

A special value usually comes from a calculation that went out of range. A number too big for a float becomes INFINITY. Zero divided by zero with Core\Math::divide gives NaN. When a special value is added to a total, the total is also a special value. So test a result with Core\Math::isFinite before you save it or show it.

To tell an infinity from NaN, use Core\Math::isNan as well. An infinity is not finite and is not NaN.

The examples below show which values are finite, and how to tell the three special values apart. The last one is a savings calculator that checks its result before it shows it.

Parameters

  • $nfloatThe value to test.

Return value

Returns bool. true for every ordinary float, zero included. false for INFINITY, -INFINITY and NaN.

Examples

Which values are finite

<?nvs
// Shows which values are ordinary numbers and which are special.
class Check
{
// Prints the label, then "finite" or "not finite".
public static function show(string $label, float $n): void
{
echo $label, ': ', Core\Math::isFinite($n) ? 'finite' : 'not finite', "\n";
}
}
// Zero and the biggest `float` are ordinary numbers.
// This prints "0: finite" and "FLOAT_MAX: finite".
Check::show('0', 0.0);
Check::show('FLOAT_MAX', Core\Math::FLOAT_MAX);
// Twice the biggest `float` does not fit, so it is `INFINITY`.
// This prints "FLOAT_MAX * 2: not finite".
Check::show('FLOAT_MAX * 2', Core\Math::FLOAT_MAX * 2.0);
// Zero divided by zero is `NaN`. This prints "0 / 0: not finite".
Check::show('0 / 0', Core\Math::divide(0.0, 0.0));
Output
0: finite
FLOAT_MAX: finite
FLOAT_MAX * 2: not finite
0 / 0: not finite

Infinity or nan

<?nvs
// Shows how to tell an ordinary number, an infinity and `NaN` apart.
class Kind
{
// Returns "a number", "infinite" or "NaN".
// An infinity is the only value that is not finite and not `NaN`.
public static function of(float $n): string
{
if (Core\Math::isFinite($n)) {
return 'a number';
}
if (Core\Math::isNan($n)) {
return 'NaN';
}
return 'infinite';
}
}
// This prints "a number".
echo Kind::of(-12.5), "\n";
// One divided by minus zero is `-INFINITY`. This prints "infinite".
echo Kind::of(Core\Math::divide(1.0, -0.0)), "\n";
// This prints "NaN".
echo Kind::of(Core\Math::sqrt(-1.0)), "\n";
Output
a number
infinite
NaN

A savings calculator

<?nvs
// Shows how a savings calculator checks its result before it shows it.
class Savings
{
// Prints what the savings are worth after some years, with interest added each year.
// A very long time gives a number too big for a `float`, so it prints an error message.
public static function printValue(float $start, float $rate, float $years): void
{
float $value = $start * (1.0 + $rate) ** $years;
if (Core\Math::isFinite($value)) {
echo 'After ', $years, ' years: ', Core\Math::round($value, { precision: 2 }), "\n";
} else {
echo 'After ', $years, " years: too big to show\n";
}
}
}
// 1000 at 5% a year for 10 years. This prints "After 10 years: 1628.89".
Savings::printValue(1000.0, 0.05, 10.0);
// The same for 20 years. This prints "After 20 years: 2653.3".
Savings::printValue(1000.0, 0.05, 20.0);
// 100000 years is too long. This prints "After 100000 years: too big to show".
Savings::printValue(1000.0, 0.05, 100000.0);
Output
After 10 years: 1628.89
After 20 years: 2653.3
After 100000 years: too big to show