Skip to content

Core\Math::divide

Divides one float by another, like the / operator. The difference is what happens when you divide by zero. The / operator throws an ArithmeticError. Core\Math::divide returns a value instead.

When you divide a number by zero, the result is INFINITY or -INFINITY. The sign comes from the signs of both numbers together, so 1.0 divided by -0.0 is -INFINITY. When you divide zero by zero, the result is NaN (a value that means "not a number").

Use it when a zero divisor is a normal case in your data and you want to handle it after the division. Core\Math::isFinite and Core\Math::isNan test the result.

The examples below show a few divisions, then the difference from the / operator, then a report that works out a rate for a page with no visits.

Parameters

  • $afloatThe number to divide.
  • $bfloatThe number to divide by. It may be zero.

Return value

Returns float. $a / $b. When $b is zero, the result is INFINITY or -INFINITY. Its sign comes from the signs of both numbers together. When both numbers are zero, the result is NaN.

Changelog

VersionChange
0.0.6Core\Math::quotient divides two whole numbers, divide divides two floats, and remainder returns what is left after a division. The old names were intDiv, fdiv and mod.

Examples

A few divisions

<?nvs
// Shows a few divisions with `Core\Math::divide`, some of them by zero.
// This prints 2.5.
echo Core\Math::divide(10.0, 4.0), "\n";
// A positive number divided by zero. This prints INF.
echo Core\Math::divide(1.0, 0.0), "\n";
// A negative number divided by zero. This prints -INF.
echo Core\Math::divide(-1.0, 0.0), "\n";
// Zero divided by zero. This prints NAN.
echo Core\Math::divide(0.0, 0.0), "\n";
Output
2.5
INF
-INF
NAN

Compared with the operator

<?nvs
// Shows how `Core\Math::divide` differs from the `/` operator when the divisor is zero.
float $zero = 0.0;
// The `/` operator throws an `ArithmeticError`. This prints "Division by zero".
try {
echo 5.0 / $zero, "\n";
} catch (ArithmeticError $error) {
echo $error->message, "\n";
}
// `Core\Math::divide` returns a value. This prints INF.
echo Core\Math::divide(5.0, $zero), "\n";
// `-$zero` is a zero with a minus sign. A positive number divided by it is
// negative, so this prints -INF.
echo Core\Math::divide(5.0, -$zero), "\n";
Output
Division by zero
INF
-INF

A rate with no visits

<?nvs
// Shows how a sales report works out the share of visits that ended in an order.
class Report
{
// Prints the share as a percentage.
// A page with no visits gives `NaN`, so it prints "no visits".
public static function printRate(string $page, float $orders, float $visits): void
{
float $rate = Core\Math::divide($orders, $visits);
if (Core\Math::isFinite($rate)) {
echo $page, ': ', Core\Math::round($rate * 100.0, { precision: 1 }), "%\n";
} else {
echo $page, ": no visits\n";
}
}
}
// This prints "Home: 2.5%".
Report::printRate('Home', 12.0, 480.0);
// This prints "Sale: 7.5%".
Report::printRate('Sale', 30.0, 400.0);
// This page is new and has no visits yet. This prints "New page: no visits".
Report::printRate('New page', 0.0, 0.0);
Output
Home: 2.5%
Sale: 7.5%
New page: no visits