3

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()?

Dmitri
  • 34,780
  • 9
  • 39
  • 55

3 Answers3

6

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() is limited to floats, 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().

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 1
    But note that bcpow does _not_ support raising a number to a decimal power. Instead, it produces a warning and gives an incorrect answer: bcscale(14); echo bcpow('10','1.3'); _bc math warning: non-zero scale in exponent_ _10_ – carbocation Dec 11 '12 at 18:37
0

According to the manual the bc* functions are

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

pow() is limited to the maximum supported numeric representation on the system it runs on.

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
0

bcpow is used for arbitrary precision values. As a third parameter you can specify a number of digits after coma.

hsz
  • 148,279
  • 62
  • 259
  • 315