|
|
|
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:
http://www.mathpath.uni.cc
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);
?>
|
| calculus of the eastersunday Categories : BC math, PHP, Date Time, Databases | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | |
| | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Reverse a given number Categories : PHP, Beginner Guides, Algorithms, Math. | | | Easy to use random number function that seeds with uniqid and allows a max value Categories : Math., PHP | | | Get the root of a number with any precision Categories : Algorithms, BC math, Math. | | | decoct -- Decimal to octal Categories : PHP, PHP Functions, Math. | | | bcadd -- Add two arbitrary precision numbers Categories : PHP, PHP Functions, BC math | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | Show the steps for converting a number from a given base to base 10. Shows the steps involved in converting a number from a given base to base 10. Categories : PHP, Math., Algorithms | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | | | A Simple Script that converts IP addresses to Color Codes -This function converts a given IP into a hex color. It changes according to ip input. Very useful to create customised pages. Categories : PHP, BC math | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | |
|
|