Signature
fpow(float $num, float $exponent): floatPurpose
fpow() raises a base to an exponent and always gives you a float. It follows IEEE 754 floating point rules, so unusual inputs produce special float values such as INF or NAN rather than stopping your script. The function is available from PHP 8.4.0.
Use it when your calculation is floating point from start to finish and you want the result type to stay predictable, whatever the inputs look like.
Signature
Parameters and defaults
num: the base. Integers are accepted and converted to float.exponent: the power to raise the base to. It may be negative, fractional or zero.
Neither parameter is optional, so there are no defaults. As with other internal functions, a value that cannot be treated as a number, such as an array, raises a TypeError.
Return value and failure cases
The return value is always a float equal to num raised to exponent. Mathematically awkward cases do not throw. They return IEEE 754 special values:
- A zero base with a negative exponent returns
INF. - A negative base with a non-integer exponent, such as
-1to the power5.5, returnsNAN. - A result too large for a float to represent overflows to
INF.
Because nothing is thrown, you detect these cases by inspecting the result with is_nan() and is_infinite().
Example 1: the three classic outcomes
This shows a normal result, an infinite result and an undefined result. var_dump() prints the float type, so you can see that even 100 is a float.
var_dump(fpow(10, 2));
var_dump(fpow(0, -3));
var_dump(fpow(-1, 5.5));float(100) float(INF) float(NAN)
Example 2: comparing with the ** operator
With two integer operands and a non-negative exponent, the ** operator returns an integer. fpow() returns a float for the same inputs. A negative exponent gives a float in both cases.
var_dump(2 ** 3);
var_dump(fpow(2, 3));
var_dump(fpow(2, -2));int(8) float(8) float(0.25)
Example 3: turning special values into messages
A small wrapper can classify the result so that callers never have to deal with INF or NAN directly. The overflow case, ten to the power 400, exceeds the largest float and becomes infinite.
function describePower(float $base, float $exponent): string
{
$result = fpow($base, $exponent);
if (is_nan($result)) {
return 'undefined';
}
if (is_infinite($result)) {
return 'infinite';
}
return (string) $result;
}
echo describePower(2, 3), "\n";
echo describePower(0, -1), "\n";
echo describePower(-8, 1 / 3), "\n";
echo describePower(10, 400), "\n";8 infinite undefined infinite
Edge cases
- Float formatting.
echo fpow(10, 2)prints100, because PHP drops the trailing.0when converting to a string.var_dump()still reportsfloat(100). - NAN never equals itself.
$result === NANis always false. Useis_nan(). - Infinity is a value, not an error. Later arithmetic with
INFkeeps producingINForNAN, so a problem can travel a long way before you notice it. Check results at the boundary where they enter your program. - Precision. Results are ordinary doubles. Large exponents lose exact integer precision, so do not use
fpow()for exact big-integer arithmetic. - Fractional exponents are roots. An exponent of
0.5is a square root, valid only for non-negative bases if you want a real number.
Common mistakes
- Expecting an integer. Code that does
fpow(2, 3) === 8is false because the result isfloat(8). Compare with8.0, or use==deliberately. - Wrapping the call in try/catch for math problems. Zero to a negative power and a negative root do not throw. Check with
is_infinite()andis_nan(). - Comparing floats for exact equality after non-trivial exponents. Use a tolerance instead.
- Assuming it replaces
**everywhere. If you want integer results for integer input, the**operator orpow()fits better. - Using it on PHP versions older than 8.4. The function does not exist there, so guard with
function_exists()or provide a fallback if you support older runtimes.
Related functions
pow()and the**operator: general exponentiation whose result type depends on the operands.fdiv(): IEEE 754 division that returnsINForNANinstead of throwing on division by zero.fmod(): floating point remainder.is_nan()andis_infinite(): tools for checking special results.
Recap
fpow($num, $exponent) computes a power using IEEE 754 semantics and always returns a float. Zero raised to a negative exponent gives INF, and a negative base with a fractional exponent gives NAN. Neither case throws, so inspect the result when inputs are not trusted. Reach for ** when you want PHP's usual integer-or-float result typing.
Tested examples
var_dump(fpow(10, 2));
var_dump(fpow(0, -3));
var_dump(fpow(-1, 5.5));
float(100) float(INF) float(NAN)
var_dump(2 ** 3);
var_dump(fpow(2, 3));
var_dump(fpow(2, -2));
int(8) float(8) float(0.25)
function describePower(float $base, float $exponent): string
{
$result = fpow($base, $exponent);
if (is_nan($result)) {
return 'undefined';
}
if (is_infinite($result)) {
return 'infinite';
}
return (string) $result;
}
echo describePower(2, 3), "\n";
echo describePower(0, -1), "\n";
echo describePower(-8, 1 / 3), "\n";
echo describePower(10, 400), "\n";
8 infinite undefined infinite