The purpose of digits() function above is to supply the characters that will be used as digits for the base you want. NOTE: You can use any characters for that when you convert to another base, but when you convert again to the decimal base, you need to use the same characters or you will get another unexpected result.

A little comment for the simplified example above: you can do base converting without BCMath functions using only math operators, but you will not able to manage very large values or work with strings to compress or scramble data. If you have BCMath installed in your system it worth use it for this.

Moreover, you might have other problems if you feed the bcmath functions directly with floating point numbers.Consider the following example:<?phpbcscale(1);$a = 0.8;$b = 0.7;var_dump((string) $a); // string(3) "0.8"var_dump((string) $b); // string(3) "0.a"var_dump(bcadd($a, $b)); // string(3) "1.5"setLocale(LC_ALL, 'fr_BE.UTF-8');var_dump((string) $a); // string(3) "0,8" --> note the commavar_dump((string) $b); // string(3) "0,7" --> note the commavar_dump(bcadd($a, $b)); // string(3) "0.0"?>The floating point numbers passed to the bcadd() function are automatically converted to string using the localized decimal separator. However, the bcmath functions always use a full stop, which results in the last result being incorrect.

Like any other bc function, you can't trust the last couple of digits, but everything else seems to check out. If you want to use this for anything important, you may want to verify this against other sources of pi before use. This function calculates 100 decimal places of pi in 329 iterations -- not exactly fast (each iteration calls the factorial function, from below, twice), so I try to avoid calling it more than once.

<?//arbitrary precision pi approximator//author tom boothby//free for any use

A found a little fix to do in my base2dec() function:The line "if($base<37) $value=strtolower($value);" should be removed if you want to specify another digits for your base conversions. Change it this way:

$mykey is a base64 value, which is a good key for passing thru an URL and also is shorter than a MD5 string (it will be allways 11 chars long). If you need something more secure, just scramble the 64 digits in the digits() function.

Oops, first posting contained wrong code... sorry.An amendment to the entry by pulstar at mail dot com - the digits() function can be made much faster (remove the line breaks from the big string, and make sure you don't miss any characters!):

I hacked these taylor expansions up to make diagrams for some physics homework. I don't think you'll be wanting to do any real science with PHP... but what the hell, why not? I plan to implement either a spigot algorithm or something similar to generate pi in the near future.

Code below implements standard rounding on 5 or higer round up, else don't round. There wasn't a round function for the BC functions, so here is a simple one that works. Same args as round, except takes strings and returns a string for more BC operations.