fpow
Raises one number to the power of another under IEEE 754 rules, always returning a float.
fpow(float $num, float $exponent): float- Added in PHP 8.4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$num | float | The base to use. |
$exponent | float | The exponent. |
Return value
floatThe floating point result of raising $num to the power of $exponent. Returns INF when $num is zero and $exponent is negative, and NAN when a negative base is raised to a non-integer exponent.
Errors
Never throws or emits a deprecation notice for a zero base with a negative exponent, unlike pow(). A TypeError is raised only if an argument cannot be coerced to float.
fpow() is the exponentiation counterpart to fdiv(): it follows IEEE 754 exactly and returns special values instead of complaining. It arrived in PHP 8.4.
How it differs from pow
pow() and the ** operator return an int when both operands are integers and the result fits, so their return type is int|float. fpow() always returns float — useful when a function signature or a numeric pipeline needs one predictable type.
The behavioural difference is at the edges. Since PHP 8.4, raising 0 to a negative exponent through pow() is deprecated and emits a notice; fpow(0, -3) simply returns INF, which is what the standard prescribes. That makes fpow() the right choice when zero bases are legitimate data rather than a bug.
What trips people up
A negative base with a non-integer exponent has no real answer, so fpow(-1, 5.5) is NAN. Test with is_nan(), never with ==. Large exponents overflow to INF silently, and both INF and NAN make json_encode() fail, so filter results with is_finite() before serialising. For everyday integer maths ** remains clearer and keeps integer precision.
Example
When to use it
- Raise a base to a power where a zero base with a negative exponent is valid input, without triggering the pow() deprecation.
- Keep a numeric pipeline strictly float-typed so downstream signatures never see an int.
- Let INF and NAN propagate through a scientific calculation instead of interrupting it.
Frequently asked questions
What is the difference between fpow and pow?
pow() returns int when both arguments are ints and the result fits, while fpow() always returns float. fpow() also follows IEEE 754 for edge cases, returning INF for a zero base with a negative exponent where pow() emits a deprecation notice as of PHP 8.4.
Which PHP version added fpow?
fpow() was introduced in PHP 8.4.0. On earlier versions use pow() or the ** operator, guarding a zero base yourself.
Why does fpow return NAN?
A negative base raised to a non-integer exponent has no real-valued result, so fpow(-1, 5.5) is NAN. Check with is_nan(), since NAN never compares equal to anything.
More examples
Edge cases side by side
0 to the power 0 is 1 by IEEE convention, 0 to a negative power is INF, and a negative base with the fractional exponent 1/3 is NAN even though the cube root of -8 is -2 in real arithmetic. The last row overflows to INF.
<?php
$cases = [[2, 3], [2, -3], [0, 0], [0, -2], [-8, 1/3], [-8, 3], [10, 400]];
foreach ($cases as [$b, $e]) {
$r = fpow($b, $e);
printf("fpow(%-4s, %-6.4g) = %s\n", $b, $e,
is_nan($r) ? 'NAN' : (is_infinite($r) ? 'INF' : sprintf('%.6g', $r)));
}Filtering non-finite results before output
json_encode() cannot represent INF or NAN and returns false for the raw array. Replacing non-finite values with null yields [256,null,null], which is the usual way to get fpow() output into an API response.
<?php
$results = [fpow(2, 8), fpow(0, -1), fpow(-4, 0.5)];
var_dump(json_encode($results)); // bool(false)
$safe = array_map(fn($v) => is_finite($v) ? $v : null, $results);
echo json_encode($safe), "\n"; // [256,null,null]
echo json_last_error_msg(), "\n";Compound growth with a float exponent
Fractional exponents work as smoothly as whole ones, so a 7.25-year holding period needs no special handling. The base is always positive here, which is what keeps every result a real number rather than NAN.
<?php
$principal = 5000.0;
$rate = 0.043;
foreach ([0.5, 1, 7.25, 30] as $years) {
printf("%5.2f yr: %10.2f\n", $years, $principal * fpow(1 + $rate, $years));
}
Discussion