Search code examples
phpexponential

PHP - weird, non-consistent results from pow()


I have this line in 2 different files but in the same application.

pow(36, 8);

One returns -INF while the other returns 2821109907456

Is there any external factors that I am not aware of that causes this? Because I don't see the inputs as the problem since they are exactly the same.

EDIT: The code which returns -INF is enclosed in a foreach. Thought that might give us a clue why this happens.

EDIT 2: The loop looks like this

foreach($items as &$item)
{
  foo($item['some_index'], ... , ..., ...);
}
unset($item);

The function that the loop calls is something like

function foo($input, $a, $b, $c)
{
   ....
   $x = pow(36, 8);
   ...
}

Solution

  • The size of an integer is platform-dependent and you may be reaching the max. Try using the BC Math function bcpow in place of pow