/*
   ###########################################################################
   This functions converts a decimal fraction into the smallest possible
   reduced form expressed in integers.

   A decimal fraction expression like:
   'A / B' = '1.8436572 / 183.4657209'
   Will be converted into the smallest possible integer fraction expression
   exactly equivalent to the same ratio.
   'A / B' = '1.8436572 / 183.4657209' ---> 'a / b' = '2956 / 294157'
   ###########################################################################
*/

   function BC_Dec_Frac_to_Int_Frac($DecFracStr)
  {
// Read decimal fraction argument string.
   $f = trim($DecFracStr);

// If argument is a single number, then append '/1.0'
// to force it into fractional form.
   if (Is_Numeric($f)) {$f .= " / 1.0";}

// Split fraction unto numerator and denominator (A/B).
   list($A, $B) = PReg_Split("[\/]", $f);
   $A = trim($A);   $B = trim($B);

// If (A) is an integer, append '.0' to make it a decimal value.
   $A .= (StrPos($A, '.') === FALSE)? '.0':'';

// If (B) is an integer, append '.0' to make it a decimal value.
   $B .= (StrPos($B, '.') === FALSE)? '.0':'';

// Determine common multiplier to make both (A,B) into integer values.
// This is determined by the value with the most decimals.
   $Ad = StrLen(StrRChr($A, '.')) - 1;
   $Bd = StrLen(StrRChr($B, '.')) - 1;
   $maxD = max($Ad, $Bd);
   $CM = '1' . Str_Repeat('0', $maxD);

   $A = $oA = bcMul($A, $CM);
   $B = $oB = bcMul($B, $CM);

   $A = Str_Replace('-', '', Str_Replace('+', '', $A));
   $B = Str_Replace('-', '', Str_Replace('+', '', $B));

// Reduce raw 'A / B' to lowest terms 'a / b'
   if (bcComp($B,$A) < 0) {$w=$B;  $B=$A;  $GCD=$w;} else {$GCD=$A;}

   while ($B <> 0) {$w=$B;   $B=bcMod($GCD,$B);   $GCD=$w;}

   $a = bcDiv($oA, $GCD);
   $b = bcDiv($oB, $GCD);

   return "$a / $b";

  } // End of  BC_Dec_Frac_to_Int_Frac (...)