Mathematics7 min readExamples run on PHP 8.5.10

fpow()

Learn how PHP 8.4 fpow() raises a float to a power, returns INF or NAN instead of throwing, and how it differs from pow() and the ** operator.

Signature

fpow(float $num, float $exponent): float
Available since PHP 8.4.07.48.08.18.28.38.48.5All versions →

Purpose

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 -1 to the power 5.5, returns NAN.
  • 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.

Example
<?php
var_dump(fpow(10, 2));
var_dump(fpow(0, -3));
var_dump(fpow(-1, 5.5));
Output
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.

Example
<?php
var_dump(2 ** 3);
var_dump(fpow(2, 3));
var_dump(fpow(2, -2));
Output
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.

Example
<?php
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";
Output
8
infinite
undefined
infinite

Edge cases

  • Float formatting. echo fpow(10, 2) prints 100, because PHP drops the trailing .0 when converting to a string. var_dump() still reports float(100).
  • NAN never equals itself. $result === NAN is always false. Use is_nan().
  • Infinity is a value, not an error. Later arithmetic with INF keeps producing INF or NAN, 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.5 is a square root, valid only for non-negative bases if you want a real number.

Common mistakes

  1. Expecting an integer. Code that does fpow(2, 3) === 8 is false because the result is float(8). Compare with 8.0, or use == deliberately.
  2. 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() and is_nan().
  3. Comparing floats for exact equality after non-trivial exponents. Use a tolerance instead.
  4. Assuming it replaces ** everywhere. If you want integer results for integer input, the ** operator or pow() fits better.
  5. 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.
  • pow() and the ** operator: general exponentiation whose result type depends on the operands.
  • fdiv(): IEEE 754 division that returns INF or NAN instead of throwing on division by zero.
  • fmod(): floating point remainder.
  • is_nan() and is_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

The three classic outcomes: normal, INF and NAN
<?php
var_dump(fpow(10, 2));
var_dump(fpow(0, -3));
var_dump(fpow(-1, 5.5));
Output
float(100)
float(INF)
float(NAN)
fpow() versus the ** operator
<?php
var_dump(2 ** 3);
var_dump(fpow(2, 3));
var_dump(fpow(2, -2));
Output
int(8)
float(8)
float(0.25)
Turning special values into messages
<?php
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";
Output
8
infinite
undefined
infinite