Search code examples
phpbcmath

What is the difference between bcpow and pow?


Can someone explain to me if I should use bcpow() instead of pow() and why?

I understand that not all installations of php have bcmath enabled. So if I write an open source project, and want to have as few dependencies/requirements as possible, I would rather use pow() in my code.

But what are the downsides to using pow() over bcpow()?


Solution

  • bcpow() is a function of the BCMath Arbitrary Precision Mathematics library.

    Quoting the introduction of it's manual :

    For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.


    On the other hand, [**`pow()`**][3] is limited to [floats][4], which have a limited size *(quoting)* :

    The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format)


    Generally, you'll work with `pow()` and other float-based functions *(which are probably faster, and are always enabled)* ; but, if you need to handle very big number, you'll have to work with `bcpow()`.