{"id":65,"date":"2019-08-30T19:52:51","date_gmt":"2019-08-30T19:52:51","guid":{"rendered":"https:\/\/tutorialio.com\/?p=65"},"modified":"2019-08-30T19:52:51","modified_gmt":"2019-08-30T19:52:51","slug":"arbitrary-precision-maths-functions-in-php-bc-math","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/arbitrary-precision-maths-functions-in-php-bc-math\/","title":{"rendered":"Arbitrary Precision Maths Functions in PHP (BC Math)"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>Arbitrary Precision Maths Functions in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#bcadd\">bcadd()<\/a><\/td>\n<td>Add two arbitrary precision numbers<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bccomp\">bccomp()<\/a><\/td>\n<td>Compare two arbitrary precision numbers<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcdiv\">bcdiv()<\/a><\/td>\n<td>Divide two arbitrary precision numbers<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcmod\">bcmod()<\/a><\/td>\n<td>Get modulus of an arbitrary precision number<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcmul\">bcmul()<\/a><\/td>\n<td>Multiply two arbitrary precision numbers<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcpow\">bcpow()<\/a><\/td>\n<td>Raise an arbitrary precision number to another<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcpowmod\">bcpowmod()<\/a><\/td>\n<td>Raise an arbitrary precision number to another, reduced by a specified modulus<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcscale\">bcscale()<\/a><\/td>\n<td>Set or get default scale parameter for all bc math functions<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcsqrt\">bcsqrt()<\/a><\/td>\n<td>Get the square root of an arbitrary precision number<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#bcsub\">bcsub()<\/a><\/td>\n<td>Subtract one arbitrary precision number from another<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"bcadd\">PHP <code>bcadd()<\/code> Function<\/h2>\n<h3>PHP <code>bcadd()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcadd()<\/code><\/code> function will sums <code class=\"parameter\">left_operand<\/code> and <code class=\"parameter\">right_operand<\/code>.<\/p>\n<h3>PHP <code>bcadd()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcadd ( string $left_operand , string $right_operand [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcadd()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>left_operand<\/code> \u2014 The left operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>right_operand<\/code> \u2014 The right operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcadd()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcadd()<\/code> function returns the sum of the two operands, as a string.<\/p>\n<h3>PHP <code>bcadd()<\/code> Working Examples<\/h3>\n<h4>1. bcadd() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$a = '1.234';\n$b = '5';\necho bcadd($a, $b);     \/\/ 6\necho bcadd($a, $b, 4);  \/\/ 6.2340\n?&gt;<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Nitrogen<\/p>\n<pre><code>I made this to add an unlimited size of numbers together.. \n \nThis could be useful for those without the BCMath extension. \n \nIt allows decimals, and optional $Scale parameter.  If $Scale isn't specified, then it'll automatically adjust to show the correct number of decimals. \n \n<br>&lt;?php \n \nfunction Add($Num1,$Num2,$Scale=null) { \n  \/\/ check if they're valid positive numbers, extract the whole numbers and decimals \n  if(!preg_match(\"\/^\\+?(\\d+)(\\.\\d+)?$\/\",$Num1,$Tmp1)|| \n     !preg_match(\"\/^\\+?(\\d+)(\\.\\d+)?$\/\",$Num2,$Tmp2)) return('0'); \n \n  \/\/ this is where the result is stored \n  $Output=array(); \n \n  \/\/ remove ending zeroes from decimals and remove point \n  $Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):''; \n  $Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):''; \n \n  \/\/ calculate the longest length of decimals \n  $DLen=max(strlen($Dec1),strlen($Dec2)); \n \n  \/\/ if $Scale is null, automatically set it to the amount of decimal places for accuracy \n  if($Scale==null) $Scale=$DLen; \n \n  \/\/ remove leading zeroes and reverse the whole numbers, then append padded decimals on the end \n  $Num1=strrev(ltrim($Tmp1[1],'0').str_pad($Dec1,$DLen,'0')); \n  $Num2=strrev(ltrim($Tmp2[1],'0').str_pad($Dec2,$DLen,'0')); \n \n  \/\/ calculate the longest length we need to process \n  $MLen=max(strlen($Num1),strlen($Num2)); \n \n  \/\/ pad the two numbers so they are of equal length (both equal to $MLen) \n  $Num1=str_pad($Num1,$MLen,'0'); \n  $Num2=str_pad($Num2,$MLen,'0'); \n \n  \/\/ process each digit, keep the ones, carry the tens (remainders) \n  for($i=0;$i&lt;$MLen;$i++) { \n    $Sum=((int)$Num1{$i}+(int)$Num2{$i}); \n    if(isset($Output[$i])) $Sum+=$Output[$i]; \n    $Output[$i]=$Sum%10; \n    if($Sum&gt;9) $Output[$i+1]=1; \n  } \n \n  \/\/ convert the array to string and reverse it \n  $Output=strrev(implode($Output)); \n \n  \/\/ substring the decimal digits from the result, pad if necessary (if $Scale &gt; amount of actual decimals) \n  \/\/ next, since actual zero values can cause a problem with the substring values, if so, just simply give '0' \n  \/\/ next, append the decimal value, if $Scale is defined, and return result \n  $Decimal=str_pad(substr($Output,-$DLen,$Scale),$Scale,'0'); \n  $Output=(($MLen-$DLen&lt;1)?'0':substr($Output,0,-$DLen)); \n  $Output.=(($Scale&gt;0)?\".{$Decimal}\":''); \n  return($Output); \n} \n \n$A=\"5650175242.508133742\"; \n$B=\"308437806.831153821478770\"; \n \nprintf(\"  Add(%s,%s);\\r\\n\/\/ %s\\r\\n\\r\\n\",$A,$B,  Add($A,$B)); \nprintf(\"BCAdd(%s,%s);\\r\\n\/\/ %s\\r\\n\\r\\n\",$A,$B,BCAdd($A,$B)); \n \n\/* \n  This will produce the following.. \n    Add(5650175242.508133742,308437806.831153821478770); \n  \/\/ 5958613049.33928756347877 \n \n  BCAdd(5650175242.508133742,308437806.831153821478770); \n  \/\/ 5958613049 \n*\/ \n \n?&gt;<br> \n \nIt was a fun experience making, and thought I'd share it. \nEnjoy, \nNitrogen.<\/pre>\n<p><\/code><\/p>\n<h2 id=\"bccomp\">PHP <code>bccomp()<\/code> Function<\/h2>\n<h3>PHP <code>bccomp()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bccomp()<\/code><\/code> function will give you the result as an integer.<\/p>\n<h3>PHP <code>bccomp()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bccomp ( string $left_operand , string $right_operand [, int $scale = 0 ] ) : int<\/code><\/pre>\n<h3>PHP <code>bccomp()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>left_operand<\/code> \u2014 The left operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>right_operand<\/code> \u2014 The right operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bccomp()<\/code> Return Value<\/h3>\n<p>The PHP <code>bccomp()<\/code> function returns 0 if the two operands are equal, 1 if the <code class=\"parameter\">left_operand<\/code> is larger than the <code class=\"parameter\">right_operand<\/code>, -1 otherwise.<\/p>\n<h3>PHP <code>bccomp()<\/code> Working Examples<\/h3>\n<h4>1. bccomp() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bccomp('1', '2') . \"\\n\";   \/\/ -1\necho bccomp('1.00001', '1', 3); \/\/ 0\necho bccomp('1.00001', '1', 5); \/\/ 1\n?&gt;<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Robert Lozyniak<\/p>\n<pre><code>Beware that negative zero does not compare equal to positive zero.<\/pre>\n<p><\/code><\/p>\n<h2 id=\"bcdiv\">PHP <code>bcdiv()<\/code> Function<\/h2>\n<h3>PHP <code>bcdiv()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcdiv()<\/code><\/code> function will divides the <code class=\"parameter\">dividend<\/code> by the <code class=\"parameter\">divisor<\/code>.<\/p>\n<h3>PHP <code>bcdiv()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcdiv ( string $dividend , string $divisor [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcdiv()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>dividend<\/code> \u2014 The dividend, as a string. <\/p>\n<\/li>\n<li>\n<p><code>divisor<\/code> \u2014 The divisor, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcdiv()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcdiv()<\/code> function returns the result of the division as a string, or <code>NULL<\/code> if <code class=\"parameter\">divisor<\/code> is <strong>0<\/strong>.<\/p>\n<h3>PHP <code>bcdiv()<\/code> Working Examples<\/h3>\n<h4>1. bcdiv() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcdiv('105', '6.55957', 3);  \/\/ 16.007\n?&gt;<\/code><\/pre>\n<h2 id=\"bcmod\">PHP <code>bcmod()<\/code> Function<\/h2>\n<h3>PHP <code>bcmod()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcmod()<\/code><\/code> function will get modulus of an arbitrary precision number.<\/p>\n<h3>PHP <code>bcmod()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcmod ( string $dividend , string $divisor [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcmod()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>dividend<\/code> \u2014 The dividend, as a string. <\/p>\n<\/li>\n<li>\n<p><code>divisor<\/code> \u2014 The divisor, as a string. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcmod()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcmod()<\/code> function returns the modulus as a string, or <code>NULL<\/code> if <code class=\"parameter\">divisor<\/code> is <strong>0<\/strong>.<\/p>\n<h3>PHP <code>bcmod()<\/code> Working Examples<\/h3>\n<h4>1. bcmod() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nbcscale(0);\necho bcmod( '5',  '3'); \/\/  2\necho bcmod( '5', '-3'); \/\/  2\necho bcmod('-5',  '3'); \/\/ -2\necho bcmod('-5', '-3'); \/\/ -2\n?&gt;<\/code><\/pre>\n<h4>2. bcmod() with decimals<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nbcscale(1);\necho bcmod('5.7', '1.3'); \/\/ 0.5 as of PHP 7.2.0; 0 previously\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP bcmod()<\/code> Function<\/h3>\n<p><strong>7.2.0 \u2014 <\/strong> <code class=\"parameter\">dividend<\/code> and <code class=\"parameter\">divisor<\/code> are no longer truncated to integer, so now the behavior of <code>bcmod()<\/code> follows <code>fmod()<\/code> rather than the % operator. <\/p>\n<p><strong>7.2.0 \u2014 <\/strong> The <code class=\"parameter\">scale<\/code> parameter was added. <\/p>\n<h2 id=\"bcmul\">PHP <code>bcmul()<\/code> Function<\/h2>\n<h3>PHP <code>bcmul()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcmul()<\/code><\/code> function will multiply two arbitrary precision numbers.<\/p>\n<h3>PHP <code>bcmul()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcmul ( string $left_operand , string $right_operand [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcmul()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>left_operand<\/code> \u2014 The left operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>right_operand<\/code> \u2014 The right operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcmul()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcmul()<\/code> function returns the result as a string.<\/p>\n<h3>PHP <code>bcmul()<\/code> Working Examples<\/h3>\n<h4>1. bcmul() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcmul('1.34747474747', '35', 3); \/\/ 47.161\necho bcmul('2', '4'); \/\/ 8\n?&gt;<\/code><\/pre>\n<h4>2. bcmul() scale example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcmul('5', '2', 2);     \/\/ prints \"10\", not \"10.00\"\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP bcmul()<\/code> Function<\/h3>\n<p><strong>7.3.0 \u2014 <\/strong> <code>bcmul()<\/code> now returns numbers with the requested scale. Formerly, the returned numbers may have omitted trailing decimal zeroes. <\/p>\n<h3>Important Points about PHP <code>bcmul()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> Before PHP 7.3.0 <code>bcmul()<\/code> may return a result with fewer digits after the decimal point than the <code class=\"parameter\">scale<\/code> parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the <code class=\"parameter\">scale<\/code>. For example: <\/p>\n<\/li>\n<pre><code class=\"lang-php\">&lt;?php\necho bcmul('5', '2', 2);     \/\/ prints \"10\", not \"10.00\"\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2 id=\"bcpow\">PHP <code>bcpow()<\/code> Function<\/h2>\n<h3>PHP <code>bcpow()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcpow()<\/code><\/code> function will raise an arbitrary precision number to another.<\/p>\n<h3>PHP <code>bcpow()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcpow ( string $base , string $exponent [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcpow()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>base<\/code> \u2014 The base, as a string. <\/p>\n<\/li>\n<li>\n<p><code>exponent<\/code> \u2014 The exponent, as a string. If the exponent is non-integral, it is truncated. The valid range of the exponent is platform specific, but is at least -2147483648 to 2147483647. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcpow()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcpow()<\/code> function returns the result as a string.<\/p>\n<h3>PHP <code>bcpow()<\/code> Working Examples<\/h3>\n<h4>1. bcpow() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcpow('4.2', '3', 2); \/\/ 74.08\n?&gt;<\/code><\/pre>\n<h4>2. bcpow() scale example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcpow('5', '2', 2);     \/\/ prints \"25\", not \"25.00\"\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP bcpow()<\/code> Function<\/h3>\n<p><strong>7.3.0 \u2014 <\/strong> <code>bcpow()<\/code> now returns numbers with the requested scale. Formerly, the returned numbers may have omitted trailing decimal zeroes. <\/p>\n<h3>Important Points about PHP <code>bcpow()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> Before PHP 7.3.0 <code>bcpow()<\/code> may return a result with fewer digits after the decimal point than the <code class=\"parameter\">scale<\/code> parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the <code class=\"parameter\">scale<\/code>. For example: <\/p>\n<\/li>\n<pre><code class=\"lang-php\">&lt;?php\necho bcpow('5', '2', 2);     \/\/ prints \"25\", not \"25.00\"\n?&gt;<\/code><\/pre>\n<\/li>\n<\/ol>\n<h2 id=\"bcpowmod\">PHP <code>bcpowmod()<\/code> Function<\/h2>\n<h3>PHP <code>bcpowmod()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcpowmod()<\/code><\/code> function will raise an arbitrary precision number to another, reduced by a specified modulus.<\/p>\n<h3>PHP <code>bcpowmod()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcpowmod ( string $base , string $exponent , string $modulus [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcpowmod()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>base<\/code> \u2014 The base, as an integral string (i.e. the scale has to be zero). <\/p>\n<\/li>\n<li>\n<p><code>exponent<\/code> \u2014 The exponent, as an non-negative, integral string (i.e. the scale has to be zero). <\/p>\n<\/li>\n<li>\n<p><code>modulus<\/code> \u2014 The modulus, as an integral string (i.e. the scale has to be zero). <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcpowmod()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcpowmod()<\/code> function returns the result as a string, or <code>FALSE<\/code> if <code class=\"parameter\">modulus<\/code> is <strong>0<\/strong> or <code class=\"parameter\">exponent<\/code> is negative.<\/p>\n<h3>Important Points about PHP <code>bcpowmod()<\/code> Function<\/h3>\n<ol>\n<li>\n<p> Because this method uses the modulus operation, numbers which are not positive integers may give unexpected results. <\/p>\n<\/li>\n<\/ol>\n<h2 id=\"bcscale\">PHP <code>bcscale()<\/code> Function<\/h2>\n<h3>PHP <code>bcscale()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcscale()<\/code><\/code> function will set or get default scale parameter for all bc math functions.<\/p>\n<h3>PHP <code>bcscale()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcscale ( int $scale ) : int<\/code><\/pre>\n<pre><code class=\"lang-php\"> bcscale ( void ) : int<\/code><\/pre>\n<h3>PHP <code>bcscale()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>scale<\/code> \u2014 The scale factor. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcscale()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcscale()<\/code> function returns the old scale when used as setter. Otherwise the current scale is returned.<\/p>\n<h3>PHP <code>bcscale()<\/code> Working Examples<\/h3>\n<h4>1. bcscale() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ default scale : 3\nbcscale(3);\necho bcdiv('105', '6.55957'); \/\/ 16.007\n\/\/ this is the same without bcscale()\necho bcdiv('105', '6.55957', 3); \/\/ 16.007\n?&gt;<\/code><\/pre>\n<h3>Changelog for PHP bcscale()<\/code> Function<\/h3>\n<p><strong>7.3.0 \u2014 <\/strong> <code>bcscale()<\/code> can now be used to get the current scale factor; when used as setter, it now returns the old scale value. Formerly, <code class=\"parameter\">scale<\/code> was mandatory, and <code>bcscale()<\/code> always returned <code>TRUE<\/code>. <\/p>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: sicerwork<\/p>\n<pre><code>Executing bcsacle() will change the scale value of fpm.conf, not only the current process.<\/pre>\n<p><\/code><\/p>\n<p>Contributed By: mwgamera<\/p>\n<pre><code>These functions DO NOT round off your values. No arbitrary precision libraries do it this way. It stops calculating after reaching scale of decimal places, which mean that your value is cut off after scale number of digits, not rounded. To do the rounding use something like this:\n<br>&lt;?php\n        function bcround($number, $scale=0) {\n                $fix = \"5\";\n                for ($i=0;$i&lt;$scale;$i++) $fix=\"0$fix\";\n                $number = bcadd($number, \"0.$fix\", $scale+1);\n                return    bcdiv($number, \"1.0\",    $scale);\n        }\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"bcsqrt\">PHP <code>bcsqrt()<\/code> Function<\/h2>\n<h3>PHP <code>bcsqrt()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcsqrt()<\/code><\/code> function will get the square root of an arbitrary precision number.<\/p>\n<h3>PHP <code>bcsqrt()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcsqrt ( string $operand [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcsqrt()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>operand<\/code> \u2014 The operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcsqrt()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcsqrt()<\/code> function returns the square root as a string, or <code>NULL<\/code> if <code class=\"parameter\">operand<\/code> is negative.<\/p>\n<h3>PHP <code>bcsqrt()<\/code> Working Examples<\/h3>\n<h4>1. bcsqrt() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho bcsqrt('2', 3); \/\/ 1.414\n?&gt;<\/code><\/pre>\n<h2 id=\"bcsub\">PHP <code>bcsub()<\/code> Function<\/h2>\n<h3>PHP <code>bcsub()<\/code> Usage<\/h3>\n<p>The PHP <code><code>bcsub()<\/code><\/code> function will subtract one arbitrary precision number from another.<\/p>\n<h3>PHP <code>bcsub()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> bcsub ( string $left_operand , string $right_operand [, int $scale = 0 ] ) : string<\/code><\/pre>\n<h3>PHP <code>bcsub()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>left_operand<\/code> \u2014 The left operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>right_operand<\/code> \u2014 The right operand, as a string. <\/p>\n<\/li>\n<li>\n<p><code>scale<\/code> \u2014 This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the <code>bcscale()<\/code> function, or fallback to 0 if this has not been set.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>bcsub()<\/code> Return Value<\/h3>\n<p>The PHP <code>bcsub()<\/code> function returns the result of the subtraction, as a string.<\/p>\n<h3>PHP <code>bcsub()<\/code> Working Examples<\/h3>\n<h4>1. bcsub() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$a = '1.234';\n$b = '5';\necho bcsub($a, $b);     \/\/ -3\necho bcsub($a, $b, 4);  \/\/ -3.7660\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Arbitrary Precision Maths Functions in PHP Function Name Function Description bcadd() Add two arbitrary precision numbers bccomp() Compare two arbitrary precision numbers bcdiv() Divide two arbitrary precision numbers bcmod() Get modulus of an arbitrary precision number bcmul() Multiply two arbitrary precision numbers bcpow() Raise an arbitrary precision number to another bcpowmod() Raise an arbitrary precision [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8,5],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-reference","tag-math","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}