Arbitrary-Precision Numbers PHP

Doubles are usually sufficiently precise for any numerical analysis you may wish to perform. However, PHP offers a way to work with numbers of much higher precision. The functions in this section use strings to store very long floating-point numbers. They each use a scale value that is the number of digits to the right of the decimal point. The scale argument that appears in all of the functions is optional and will override the default scale.

These functions are activated when PHP is compiled. They are part of the binary distribution for windows, but they are not activated by default for other operating systems. If PHP reports these functions as being unrecognized, you may need to recompile PHP using the —enable-bcmath option.

string bcadd(string left, string right, integer scale)
The bcadd function adds left to right.

<?
print(bcadd("1.234567890", "9.87654321", 10));
?>

integer bccomp(string left, string right, integer scale)
The bccomp function compares left to right. If they are equal, zero is returned. If left is less than right, -1 is returned. If left is greater than right, 1 is returned.

<?
print(bccomp("12345","1.111111111111", 10));
?>

string bcdiv(string left, string right, integer scale)
Use bcdiv to divide left by right.

<?
print(bcdiv("12345", "98754", 10));
?>

string bcmod(string left, string right)
The bcmod function finds the modulus of the division of left by right.

<?
print(bcmod("66394593", "133347"));
?>

string bcmul(string left, string right, integer scale)
Use bcmul to multiply the left argument and the right argument.

<?
print(bcmul("66394593", "133347", 10));
?>

string bcpow(string value, string exponent, integer scale)
The bcpow function raises the value argument to the power of the exponent argument. If the exponent is not an integer, the fractional part will be chopped off.

<?
print(bcpow("66394593", "3", 10));
?>

string bcsqrt(string value, integer scale)
The bcsqrt function returns the square root of the value argument.

<?
print(bcsqrt("1234.567", 10));
?>

string bcsub(string left, string right, integer scale)
Use the bcsub function to subtract the right argument from the left argument.

<?
print(bcsub("1234.4842", "88.6674"));
?>


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics