9

The following code outputs 0, which isn't correct:

$r = gmp_pow(gmp_init('-1.7976931348623157'), 308);
echo gmp_strval($r);

I was under the impression that the GMP library was capable of handling floating point numbers, or have I made a mistake in the code?

Matty
  • 33,203
  • 13
  • 65
  • 93

1 Answers1

12

GMP library was capable of handling floating point numbers,

It's not. You can test that with:

echo gmp_strval(gmp_init('18')); // 18
echo gmp_strval(gmp_init('1.8')); // 0

Now, what you could do is use BCMath instead:

$num = bcpow('-1.7976931348623157', '308');
echo $num;
echo floatval($num); // for a "prettier" format
NullUserException
  • 83,810
  • 28
  • 209
  • 234
  • This is what I found too. I checked the library's wiki page and it said otherwise. (http://en.wikipedia.org/wiki/GNU_Multi-Precision_Library) This is weird. – Matty Aug 05 '11 at 02:27
  • @Matty It seems like PHP uses GMP for arbitrary length integers only: http://php.net/manual/en/language.types.integer.php – NullUserException Aug 05 '11 at 02:30
  • 2
    Yup, you're right. http://www.php.net/manual/en/intro.gmp.php also says this. I glossed right over that because the PHP extension is supposed to be a wrapper for the GMP library. I guess it's an incomplete one. – Matty Aug 05 '11 at 02:34