Current Location: Home> Latest Articles> How to Use bcsqrt and bcscale Together? A High-Precision Calculation Example in PHP

How to Use bcsqrt and bcscale Together? A High-Precision Calculation Example in PHP

gitbox 2025-07-18

In PHP, when performing high-precision calculations, we typically use various functions provided by the bcmath extension. Among them, bcsqrt and bcscale are two very important functions. bcsqrt is used to calculate the square root of a number, while bcscale sets the global precision for high-precision operations. Understanding how to use these two functions together can help us get more accurate results when dealing with high-precision mathematical calculations.

What is bcsqrt?

The bcsqrt function calculates the square root of a high-precision number. Its basic usage is as follows:

<span><span><span class="hljs-title function_ invoke__">bcsqrt</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$num</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$scale</span></span><span> = </span><span><span class="hljs-literal">null</span></span><span>): </span><span><span class="hljs-keyword">string</span></span>
</span></span>
  • $num: The number (in string format) whose square root is to be calculated.

  • $scale: Optional parameter specifying the precision of the returned result.

If no precision is specified, bcsqrt will use the current global precision, which is usually set by bcscale.

What is bcscale?

The bcscale function sets the global precision for numbers. It controls the default number of decimal places for all high-precision operations (including addition, subtraction, multiplication, division, etc.). Its basic usage is as follows:

<span><span><span class="hljs-title function_ invoke__">bcscale</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$scale</span></span><span>): </span><span><span class="hljs-keyword">void</span></span>
</span></span>
  • $scale: The number of decimal places to set.

When you perform high-precision operations such as bcsqrt, if no precision is specified for the operation, it will use the global precision set by bcscale.

How to use bcsqrt and bcscale together?

In high-precision calculations, the default precision of bcsqrt results usually depends on the global precision set by bcscale. If you want to get a more precise square root result, you can increase the precision by adjusting bcscale, or directly specify the scale parameter in bcsqrt.

Example 1: Setting global precision with bcscale

<span><span><span class="hljs-comment">// Set global precision to 10 decimal places</span></span>
</span><span><span class="hljs-title function_ invoke__">bcscale</span></span><span>(</span><span><span class="hljs-number">10</span></span><span>);
<p></span>// Calculate the square root of a number<br>
</span>$number = "123.456";<br>
</span>$result = bcsqrt($number);</p>
<p>// Output the result<br>
echo "Square root result: " . $result . "\n";<br>

In this example, we set the global precision to 10 decimal places with bcscale(10). Then, we use bcsqrt to calculate the square root of 123.456, and the result retains 10 decimal places.

Example 2: Specifying precision directly in bcsqrt

Sometimes you might want to specify precision for a particular operation without affecting the global settings. In this case, you can directly pass the scale parameter to the bcsqrt function:

<span><span><span class="hljs-comment">// Set global precision to 2 decimal places</span></span>
</span><span><span class="hljs-title function_ invoke__">bcscale</span></span><span>(</span><span><span class="hljs-number">2</span></span><span>);
<p></span>// Calculate the square root of a number, specifying the result precision as 6 decimal places<br>
</span>$number = "123.456";<br>
</span>$result = bcsqrt($number, 6);</p>
<p>// Output the result<br>
echo "Square root result: " . $result . "\n";<br>

In this example, although the global precision is set to 2 decimal places, we specify 6 decimal places for the square root calculation by passing the scale parameter directly to bcsqrt. This way, the result is accurate to 6 decimal places without being affected by the global precision.

Example 3: Combining bcsqrt with other high-precision operations

bcsqrt is often used together with other high-precision operations, such as bcadd, bcdiv, etc., to perform complex calculations. Here is an example combining bcsqrt and bcadd:

<span><span><span class="hljs-comment">// Set global precision to 5 decimal places</span></span>
</span><span><span class="hljs-title function_ invoke__">bcscale</span></span><span>(</span><span><span class="hljs-number">5</span></span><span>);
<p></span>// Calculate the square roots of two numbers and their sum<br>
</span>$number1 = "25.0";<br>
$number2 = "16.0";</p>
<p>// Calculate the square roots<br>
$sqrt1 = bcsqrt($number1, 5);<br>
$sqrt2 = bcsqrt($number2, 5);</p>
<p>// Calculate the sum of the square roots<br>
$sum = bcadd($sqrt1, $sqrt2, 5);</p>
<p>// Output the results<br>
echo "Square root 1: " . $sqrt1 . "\n";<br>
echo "Square root 2: " . $sqrt2 . "\n";<br>
echo "Sum of square roots: " . $sum . "\n";<br>

In this example, we first calculate the square roots of 25.0 and 16.0, then use bcadd to add them. The entire calculation uses a precision of 5 decimal places.

Summary

By combining bcsqrt and bcscale, we can perform high-precision calculations more accurately. In practice, bcscale controls the global precision, while bcsqrt allows specifying precision for each square root calculation. This flexible combination helps us achieve the required precision when handling financial calculations, scientific computing, and other fields.