Search code examples
phpgmppow

Power big float numbers (100.33^360)


I have problem:

pow(100.33, 360); // return INF

I use GMP, but this not get float number:

gmp_strval(gmp_pow('2', 3)); //return 8
gmp_strval(gmp_pow('2.0', 3)); //return 0

Please help :) How to do it?


Solution

  • From the PHP GMP documentation page:

    These functions allow you to work with arbitrary-length INTEGERS using the GNU MP library.

    In other words, no floating point allowed. GMP itself can do floating point but it appears that the PHP interface to it doesn't use its full power.

    You can use bcpow to get this functionality with something like:

    $num = bcpow('100.33', '360', 20);
    echo $num;
    

    and this outputs:

    3274103534834396431867559103093187830103180578316133866868
    8735096209772372540787863291005903612118038812972585250990
    9184783128175191070195558359762317865469719418788175465726
    2260403727769361000933644502616255136475528818978181028888
    7900899387124705985519483201689097535257257471686279645479
    0293787644875616262913286836073610372960697383687245477891
    3127091993585718764278131719726387787145646698001143323773
    8827166195539744295014989026208988133960971242873367417385
    5744373185506374857530249495974084141129611541831378797038
    9257147348718093640732700784857982966810730374711336160716
    5240002574688357771367833219551029582525497843733924244263
    5089812833806492039068418216603999851813542524325083024650
    6447037300606418104334363.93719644178064144117