PDA

View Full Version : All Your Base Are Belong To Us



JayT
09-05-2007, 10:38 PM
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.





$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:




/*

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

Ezekiel
09-06-2007, 04:13 AM
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.

JayT
09-06-2007, 01:34 PM
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.


:)

Ezekiel
09-07-2007, 05:44 AM
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:

http://www.exoteric.ws/forum/



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.

JayT
09-08-2007, 03:17 PM
I can help if you want -- I've installed forums before. See:

http://www.exoteric.ws/forum/



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.

:)