Miscellaneous Code

  Home arrow Miscellaneous Code arrow BCroot, get the root of a number with ...
MISCELLANEOUS CODE

BCroot, get the root of a number with BC functions
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-01-31

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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.


    By : Mgccl

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

    //THE EXAMPLE
    $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
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap