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