At times we require precision arithmetic that goes beyond the capacities of calculators. On Unix systems, the utility dc (desk calculator) fills the gap. However, if the calculation is to be integrated in a PHP calculation, BCMATH routines are ready to help.
This example demonstrates the routines using an HTML form interface to call a PHP script which operates on the data supplied by the visitor. Operations include the usual four operations (+,-,*,/), square root, modulo and powers. The last three operators are abbreviated by the letters s, m and p respectively.
The precision parameter allows the user to determine the number of significant digits to be display in the output. I have not been able to test the script on different implementations of PHP, but on the one to which I have access, it took less than ten seconds to get results of a square root to 10000 digits, although the same system truncates constants (pi) to 4000 digits.
For those who may not have access to PHP on their personal computers, they could try a fully functional version at the following site:
Feel free to fill your comments forms at the above site for your comments, suggestions, and bug reports. Many thanks are due to Jeremy Watts who revived my interest on this subject through his excellent contributions at this forum.
Here is the submitted code.
<html>
<head>
<title>bcmath (FILE bcm.htm)</title>
</head>
<body>
<h1>Use of BCMATH Routines to do Arbitrary Precision Arithmetic</h1>
<p>The operators available are:
<table rules="all">
<th>operator</th><th>remarks</th>
<tr><td>+</td><td>Addition, arg1+arg2</td></tr>
<tr><td>-</td><td>Subtraction, arg1-arg2</td></tr>
<tr><td>*</td><td>Multiplication, arg1*arg2</td></tr>
<tr><td>/</td><td>Division, arg1/arg2</td></tr>
<tr><td>s</td><td>Square Root, sqrt(arg1), (only first argument used)</td></tr>
<tr><td>p</td><td>power, arg1**arg2</td></tr>
<tr><td>m</td><td>modulus, arg1 mod arg2</td></tr>
</table>
<br>
<h2>Input precision, numbers and operator (+ - * / s=sqrt p=power m=modulus)</h2>
<form action="bcm.php" method="post">
Precision: <input type="text" name="prec" size=3 value="20"><br>
Expression(argument1,operator,argument2):<br>
<input type="text" name="arg1" size="50">
<input type="text" name = "oper" size="1">
<input type="text" name="arg2" size="50">
<input type="submit" value="Calculate">
</form>
<h4>If you do not have access to PHP, visit <a href="http://www.mathpath.uni.cc">here</a>, click on Mathpath followed by Arbitrary Precision Arithmetic (PC 2004-05-23)</h4>
</body>
</html>
<?php
// PHP script to do arbitrary precision arithmetic using BCMATH routines
// File bcm.php by PC, 2004-05-23
// transcribe parameters from HTML form
$prec=$_POST['prec'];
$arg1=$_POST['arg1'];
$oper=$_POST['oper'];
$arg2=$_POST['arg2'];
// do calculations according to operator
if ($oper==="+")$ans=bcadd($arg1,$arg2,$prec);
if ($oper==="-")$ans=bcsub($arg1,$arg2,$prec);
if ($oper==="*")$ans=bcmul($arg1,$arg2,$prec);
if ($oper==="/")$ans=bcdiv($arg1,$arg2,$prec);
if ($oper==="p")$ans=bcpow($arg1,$arg2,$prec);
if ($oper==="s")$ans=bcsqrt($arg1,$prec);
if ($oper==="m")$ans=bcmod($arg1,$arg2);
// print results
printf("%s %s %s =%s",$arg1,$oper,$arg2,$ans);
?>