fdiv

Divides two numbers under IEEE 754 rules, returning INF or NAN instead of throwing on division by zero.

Signature
fdiv(float $num1, float $num2): float
  • Added in PHP 8.0

Parameters

ParameterTypeDescription
$num1floatThe dividend (numerator).
$num2floatThe divisor.

Return value

floatThe floating point result of $num1 / $num2. When $num2 is zero the result is INF, -INF or NAN depending on the sign of $num1.

Errors

Never throws DivisionByZeroError. A TypeError is raised only if an argument is not a float or a value that can be coerced to float.

fdiv() performs plain floating-point division, but follows the IEEE 754 standard all the way: dividing by zero produces a value rather than an error. A positive dividend gives INF, a negative one gives -INF, and 0 / 0 gives NAN.

When to reach for it

The / operator throws DivisionByZeroError in PHP 8, so any expression whose divisor comes from data needs either a guard or fdiv(). Reach for it when a zero divisor is an expected input rather than a bug: averaging an empty set, computing a rate before any events have been recorded, a ratio against a counter that starts at zero. It also matters in numerical code that relies on infinities propagating naturally instead of being interrupted.

What trips people up

NAN is not equal to anything, including itself. $x == NAN and $x === NAN are both always false, so the only correct test is is_nan($x); use is_infinite() and is_finite() for the other cases.

Both arguments are coerced to float, so fdiv() never gives you integer division — fdiv(7, 2) is 3.5. Use intdiv() for that. And these special values survive arithmetic: an INF that reaches json_encode() triggers a failure, and one written into a database column will usually be rejected, so filter before you serialise.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Compute an average over a collection that may be empty without wrapping the call in a guard.
  • Calculate a conversion or click-through rate when the denominator counter can still be zero.
  • Let INF propagate through a numerical pipeline instead of interrupting it with an exception.

Frequently asked questions

What is the difference between fdiv and the / operator?

They give the same answer for a nonzero divisor. When the divisor is zero, / throws DivisionByZeroError while fdiv() returns INF, -INF or NAN per IEEE 754.

How do I check whether fdiv returned NAN?

Use is_nan($result). Comparison never works, because NAN is not equal or identical to any value, including another NAN.

Does fdiv work with integers?

Yes, but both arguments are cast to float and the return type is always float, so fdiv(7, 2) is 3.5. Use intdiv(7, 2) if you want the integer 3.

More examples

fdiv versus the division operator

The / operator raises DivisionByZeroError, while fdiv() returns INF for the same inputs. The last two lines show that fdiv() always produces a float: use intdiv() when you want truncated integer division.

Example · php
<?php
try {
    echo 1 / 0;
} catch (DivisionByZeroError $e) {
    echo "operator threw: ", $e->getMessage(), "\n";
}

var_dump(fdiv(1, 0));   // no exception
var_dump(intdiv(7, 2)); // int(3)
var_dump(fdiv(7, 2));   // float(3.5)

A conversion-rate report with empty buckets

Homepage reports 7.00% and pricing 15.00%. The beta page has zero visits, so fdiv() yields NAN, which is_nan() turns into a readable label instead of an exception halting the whole report.

Example · php
<?php
$data = [
    'homepage' => ['visits' => 1200, 'signups' => 84],
    'pricing'  => ['visits' => 300,  'signups' => 45],
    'beta'     => ['visits' => 0,    'signups' => 0],
];
foreach ($data as $page => $d) {
    $rate = fdiv($d['signups'], $d['visits']) * 100;
    printf("%-9s %s\n", $page, is_nan($rate) ? 'no traffic' : sprintf('%.2f%%', $rate));
}

Filtering specials before serialising

json_encode() returns false on the raw array because INF and NAN have no JSON representation. Mapping non-finite values to null first produces valid JSON, which is the usual fix before sending fdiv() results to an API or a log.

Example · php
<?php
$values = [fdiv(4, 2), fdiv(1, 0), fdiv(0, 0), fdiv(-1, 0)];

$safe = array_map(fn($v) => is_finite($v) ? $v : null, $values);
var_dump(json_encode($values));  // bool(false)
echo json_encode($safe), "\n";   // [2,null,null,null]
echo json_last_error_msg(), "\n";

Discussion

  • Be the first to comment on this lesson.