WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Arbitrary Precision Math using BCMATH routines
Categories : PHP, Math., BC math Click here to Update Your Picture
P C
Date : May 24th 2004
Grade : 2 of 5 (graded 3 times)
Viewed : 7699
File : 3880.htm
Images : No Images for this code example.
Search : More code by P C
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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);
?>




Latitude-Longitude to Miles
Categories : PHP, Utilities, Math.
Math operations on big numbers
Categories : PHP, 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.
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
bcadd -- Add two arbitrary precision numbers
Categories : PHP, PHP Functions, BC math
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
Calculator for Baroque Violin strings
Categories : Math., PHP, Strings
Temperature Conversion
Categories : PHP, Math., Beginner Guides