What is BCMath and why should anyone use it?
BC stand for Binary Calculator. BCMath can break free from PHP's limit on numbers and theoretically possible on calculating infinite large and infinite precision numbers. It receives numbers as strings and output them as string. There is not much of a reason to use BCMath, because PHP are not made to do those very mathy calculations. But if you want, this tutorial is for you.
What are the functions in BCMath?
There are only 10 functions in BCMath, there are all basic ones, you can find the equivalent in PHP math operators.
bcadd
Adds two numbers together, equivalent to "+". Optional third parameter choses how much precision the function is going to calculate.
Example:
bcadd('333','666');//return 999
bcadd('0.9','0.01',1);//return 0.9
bccomp
Compare two numbers, return a integer value states the 1nd value is larger(1), equal(0) or smaller(-1). Optional third parameter choses how much precision the function is going to count.
Example:
bccomp('30','50');//return -1
bccomp('50','30');//return 1
bccomp('50.05','50',1);//return 0
bcdiv
Divide the first number to the 2nd number, return a string result. Equivalent to "/". Optional third parameter choses how much precision the function is going to count.
Example:
bcdiv('1','3',3);//return 0.333
bcmod
Find the modules of a number. first number is the number get module by the 2nd one. return a string. Equivalent to "%".
Example:
bcmod('1','3');//return 0
bcmul
Multiply the first number to the second one. Return a string. Optional third parameter choses how much precision the function is going to count. Equivalent to "*".
Example:
bcmul('1','3',5);return 3.00000
bcpow
The first number is the base and the second is the exponent. Optional third parameter choses how much precision the function is going to count. Equivalent to the pow() in PHP. Note that the function only take in integers as exponents(check out this post to see how to solve the problem) unlike pow(). and the function can't have exponents lager than 2^31.
Example:
bcpow('3','2',2);//return 9.00
bcpow('3','0.9');//return 1
bcpow('3','999999999999999999999999999999999999');//return 1
bcpowmod
bcpowmod is equivalent to a bcpow then bcmod, like this:
bcpowmod($base, $exponent, $mod);
bcmod(bcpow($base, $exponent), $mod);
This function can have exponent over 2^31 and have a optional forth parameter choses how much precision the function is going to count.
bcscale
bcscale set the default precision of all bc functions. Input must be a integer, and it return true when success and false when fail.
Example:
bcsqrt
Calculate the square root of the input number, optional 2nd parameter choses how much precision the function is going to count. Equivalent to sqrt() function.
Example:
bcsqrt('2',5);//return 1.41421
bcsub
Subtract the 2nd number from the first number. optional 3nd parameter choses how much precision the function is going to count. Equivalent to "-".
Example:
bcsub('3','2',2);//return 1.00
What else do I need to know?
BCMath only have 10 functions, so you have to write your own function, like one that do sin() and cos(). That's when you will find algorithms really important.
There is another math extension called GMP, quite like BCMath but much more powerful(built in functions saves you time).
Recent comments
9 hours 58 min ago
21 hours 33 min ago
2 days 13 hours ago
2 days 23 hours ago
4 days 7 hours ago
5 days 14 hours ago
5 days 22 hours ago
6 days 11 hours ago
6 days 11 hours ago
6 days 14 hours ago