munin
+ Reply to Thread
Results 1 to 5 of 5

Thread: All Your Base Are Belong To Us

  1. #1
    Join Date
    Aug 2007
    Posts
    122

    All Your Base Are Belong To Us

    Here is a function to convert any fractional value in any base into its equivalent base *0 decimal value to any given number of decimals.

    The argument must be a positive fractional value.


    NOTE *:
    This function is case sensitive and all hexadecimal numbers with letters MUST use lowercase, but you can easily modify the program as you see fit if this is a problem.

    NOTE 2:
    The conversion results are NOT rounded.



    USAGE EXAMPLE:

    To convert a base ** fractional value into its base *0 equivalent to *6 decimals.


    Code:
    $frax = "0.*a84*6b5720*c";
    
    $base = **;
    
    $decimals = *6;
    
    print bcBaseB_Frac_To_Base*0 ($frax, $base, $decimals);

    So, the base ** fraction 0.*a84*6b5720*c

    converts to the base *0 fraction

    0.***88555*7*6**60*27060*78**8*24*45*7

    carried out to *6 decimals.



    Here is the function code:

    Code:
    /*
    
       This function converts from any positive fractional
       number expressed in any base from 2 to 62 into its
       equivalent base *0 fraction value to a specified
       number of decimals.
    
       Author: Jay Tanner &#*6*;2007
       PHP v4.4.4
    
       Released under provisions of GPL v*
       http://www.gnu.org/licenses
    
       =========
       ARGUMENTS
    
       $FraxArg     = Fractional argument in any base
       $BaseArg     = Base of the given fraction value
       $DecimalsArg = Decimals in converted base *0 result
    
    
       ====
       NOTE
    
       In this base enumeration scheme, the letters used
       for representing higher number bases are always
       case sensitive.  The letters used for hexadecimal
       must always be expressed in lowercase letters.
    
       The uppercase letters represent bases *7 to 62.
    
    
       ======
       ERRORS
    
       An error message is returned if any of the digits
       are invalid for the given base or the base is < 2.
    
    
    */
    
    
       function bcBaseB_Frac_To_Base*0 ($FraxArg, $BaseArg, $DecimalsArg)
    
    {
    
    // ---------------------
    // Read input arguments.
    
              $x = trim($FraxArg);
              $b = IntVal(trim($BaseArg));
       $decimals = trim($DecimalsArg);
    
    // --------------------
    // Error if base is < 2
    
       if ($b < 2) {return "ERROR: Base must be > *";}
    
    // ---------------------------
    // Set index to decimal point.
    
       $i = StrPos($x, ".");
    
    // --------------------------------------------------
    // Remove anything prior to and including the decimal
    // point from the given fractional value.
    
       $x = SubStr($x, $i+*, StrLen($x));
    
    // -------------------------------------------------
    // Define digits available for the base conversions.
    // Index position of digit represents its value.
    
       $digits = "0*2*45678*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
    // --------------------------------------------
    // Test for valid base. An error results if any
    // digit within the number has a numerical value
    // greater than $b-*, which means the given digit
    // is not valid for the given base, such as the
    // digit F cannot be used in a base *0 number.
    
       $sum = "0";  $j = StrLen($x);
    
       for ($i=0;   $i < $j;   $i++)
           {
                 $digit = SubStr($x, $i, *);
            $DigitValue = StrPos($digits, $digit);
    
            if($DigitValue > ($b-*))
              {
               return "ERROR: Invalid digit [$digit] not allowed in base [$b]";
              }
           }
    
    // ------------------------------------
    // DROP THROUGH HERE AND CONTINUE BELOW
    // IF NO INVALID DIGITS DETECTED.
    
    
    
    
    // --------------------------------
    // Perform loop to account for each
    // digit in the fractional argument
    // and perform summation for each
    // converted base *0 value.
    
       $j = StrLen($x);   $PowerOfBase = *;
    
       for ($i=0;   $i < $j;   $i++)
           {
            $DigitValue  = StrPos($digits, SubStr($x, $i, *));
            $PowerOfBase = bcMul ($PowerOfBase, $b);
                      $q = bcDiv ($DigitValue, $PowerOfBase, $decimals+5);
                    $sum = bcAdd ($sum, $q, $decimals+5);
           }
    
    // -------------------------------------------------
    // Trim off any redundant zeros and/or decimal point
    // if result is shorter than the decimals limit and
    // trims off values like 0.500000000000000000... to
    // simply 0.5 instead.
    
       $sum = RTrim(RTrim(bcAdd($sum, "0", $decimals), "0"), ".");
    
    // Done.
       return $sum;
    
    } // End of  bcBaseB_Frac_To_Base*0()



    Cheers

    Jay - The Other White Meat
    Last edited by JayT; 09-07-2007 at 06:15 AM.
    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

  2. #2
    Join Date
    Sep 2005
    Posts
    2,050
    Damn, so much awesome work appearing on this forum lately. I'd ask for you to be a mod too if we hadn't already got enough.

  3. #3
    Join Date
    Aug 2007
    Posts
    122

    Yo

    Quote Originally Posted by mike*5* View Post
    Damn, so much awesome work appearing on this forum lately. I'd ask for you to be a mod too if we hadn't already got enough.
    Hi Mike:

    My web host has a forum package I've been considering installing, but haven't done it yet. It's a bit intimidating at first glance, since I never had a forum of my own and need to figure it out.

    I didn't know if math and science was a subject that would attract many visitors, so, I settled in here to see what would happen.
    I also sometimes might not have the time to maintain it and I wouldn't want to start a project and then neglect it.

    Even magic can't turn people into chickens, but math can.

    LOL


    I've been looking for an ****** to share my code. Hope there is someone here who can make use of it.


    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

  4. #4
    Join Date
    Sep 2005
    Posts
    2,050
    Quote Originally Posted by JayT View Post
    Hi Mike:

    My web host has a forum package I've been considering installing, but haven't done it yet. It's a bit intimidating at first glance, since I never had a forum of my own and need to figure it out.
    I can help if you want -- I've installed forums before. See:

    [url]http://www.exoteric.ws/forum/[/url]

    I've been looking for an ****** to share my code. Hope there is someone here who can make use of it.
    This forum gets around *00 visitors at any given time, so I say yes, there are people here who can make use of it.

    Remember to add your name/alias at the top of all your code to try to prevent idiots copying it and passing it off as their own without permission.

  5. #5
    Join Date
    Aug 2007
    Posts
    122

    re:mike*5*

    Quote Originally Posted by mike*5* View Post
    I can help if you want -- I've installed forums before. See:

    [url]http://www.exoteric.ws/forum/[/url]



    This forum gets around *00 visitors at any given time, so I say yes, there are people here who can make use of it.

    Remember to add your name/alias at the top of all your code to try to prevent idiots copying it and passing it off as their own without permission.

    I could always use a little help.

    My web host apparently removed several other previous BBS packages that were available in my CPanel - leaving only one called yaBB, but it doesn't look too bad.

    Experimenting with it now.

    Oh to be free, so blissfully free, of the ravages of intelligence, there is no greater joy! - The Cweationist's Cweed

    All that is necessary for evil to triumph is a good PR firm.
    Very funny, Scotty. Now beam down my clothes!

+ Reply to Thread

Similar Threads

  1. Another Base *0 to Other Base Converter
    By JayT in forum Programming
    Replies: 0
    Last Post: 08-31-2007, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts