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()?
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()
.
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.
bcpow
is used for arbitrary precision values. As a third parameter you can specify a number of digits after coma.
精彩评论