|
|
|
|
Like this code?
Show the author your appreciation.
|
|
| |
There are times you have to get a root of a number into very precise decimal. bcpow() does not work like pow(), it can't accept decimal numbers. Which means it can't get a root of a number. Here is a function that can do that. This system is optimized, but it's still slow when taking in a large number.
Here is the function
|
function bcgetscale(){
return strlen(bcadd(1,0))-2;
}
//Version 0.2 of BCRoot
//By Chao XU
//From www.webdevlogs.com
//Change Log: It uses a lot bcsqrt() to make the speed fast
//fix a small decimal bug, where the last decimal could be wrong
function bcroot($a, $n, $scale='default'){
$default = bcgetscale();//Get the scale
if($scale == 'default'){
$scale = $default;//use default scale
}
if($n & ($n-1)){//check if $n is the power of 2, return 0 if is
//decbin is the reason this function can't have number
//larger than 2^31
$bin = decbin($n);
$i = strlen($bin)-1;
$pow = 0;
while($i){
if($bin[$i]==='0'){
++$pow; –$i;
}else{
break;
}
}
$n = bcdiv($n, bcpow(2, $pow),0);
//now use Newton’s method to find the number
bcscale($scale+15);
$x = 1;
$k = 0;
$limit = ceil(log($scale+15)/log(2))+1;
while($k<$limit){
$t1 = bcdiv(1,$n);
$t21 = bcmul(bcsub($n,1),$x);
$t22 = bcdiv($a,bcpow($x, $n-1));
$t2 = bcadd($t21,$t22);
$x = bcmul($t1, $t2);
++$k;
}
$i = 0;
while($i < $pow){
$x = bcsqrt($x,$scale+3);
++$i;
}
bcscale($default);
return bcadd($x,0,$scale);
}else{
//here use many bcsqrt, because this is FAST
$i = 0;
$pow = log($n)/log(2);
while($i < $pow){
$a = bcsqrt($a,$scale+3);
++$i;
}
return bcadd($a,0,$scale);
}
}
?> | |
Here is how to use it:
| <?php
$the_number = 3;
$the_nth_root = 12;
$the_precision = 30;
bcroot($the_number, $the_nth_root, $the_precision);
//returns 1.095872691135244380160019128072.
//Which is the 12th root of 3
?> | | |
|
| Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP, BC math, Algorithms | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | How to judge if an integer is odd or is even in Php3? Categories : Math., PHP, Algorithms | | | Simple histogram class with a constructor and printing
methods (updated 1999/03/26) Categories : Algorithms, 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 | | | Prime Spiral is a image plotted with all the primes in a number spiral.
Categories : Algorithms, Graphics, GD image library, Math. | | | Arbitrary Precision Math using BCMATH routines Categories : PHP, Math., BC math | | | Prime number finder (Sieve of Erastothenes) Categories : PHP, Algorithms, Math. | | | Library of math functions to expand the functionality of PHP3. Version 1.2.1 fixes a major problem with the gcd function.
Categories : Algorithms, PHP, Math. | | | Math operations on big numbers Categories : PHP, Math. | | | Latitude-Longitude to Miles Categories : PHP, Utilities, Math. | | | Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | | | Dollar Serial Number Validator Categories : PHP, Security, Algorithms | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | 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 | |
|
|
|