Bluefire
Bluefire

Reputation: 14119

Very big numbers make bcpowmod overflow

I need to raise 8 to the power of 17 in PHP. I have checked this to be 2251799813685248 on my PC's calculator, but both the pow() and bcpowmod() functions seem to overflow; pow() goes into scientific notation, and echoing the results from bcpowmod(), even with a high third parameter gives me a blank screen. Is there any other way I could perform this calculation?

Upvotes: 1

Views: 641

Answers (2)

KingCrunch
KingCrunch

Reputation: 132011

Use pow(). The "scientific notation" you are talking about is just a notation and you can format it later.

echo number_format(pow(8,17), 0, '', '');

http://php.net/number-format

Beside this I couldn't reproduce the "scientific-notation"-behaviour for the given values

http://codepad.viper-7.com/AwM3CS (Uses bigger values to enforce the scientific notation)

Upvotes: 1

hsz
hsz

Reputation: 152266

You can try with gmp_pow

gmp_pow("8", 17);

With h2ooooooo suggestion - to get result use:

gmp_strval(gmp_pow("8", 17));

Also bcpow works well for me

bcpow("8", "17")

Upvotes: 3

Related Questions