Basic Fractional Arithmetic
In this section are PHP functions developed to perform arbitrary precision fractional arithmetic. Here, arbitrary precision means to virtually any number of digits. For example, we could very easily use numbers with hundreds of decimals in our computations if we wished, such as performing a computation taken out to 100 or 500 decimals or an even higher arbitrary precision level, within the resources of the computer.
In order to work with integer fractions, we need to establish the basic algebraic rules of fractional arithmetic. These algebraic rules will still hold true even when the numeric values are nonintegers. Given two integer fractions: and , we add, subtract, multiply or divide the fractions and symbolize the raw result as . We can then attempt to reduce it via the GCD to a smaller fraction symbolized as where, = For more advanced usage, decimal ratios could first be converted into equivalent integer ratios by simply shifting the decimals of (multiplying by 10) the numerator and denominator uniformly until both become integer values. For example, the fractional ratio, 1.28/2.7 , could be replaced by 128/270 , which is exactly equivalent, by simply shifting the decimals in both the numerator and denominator by 2 places, in this case, multiplying both values by 100, to remove any decimal fractions. Then this result can also be reduced to 64/135 . In other words, we can replace the fraction 1.28/2.70 with 64/135, its exact integer equivalent.
Fraction Addition Rule: /* This PHP function performs arbitrary precision integer fraction addition without reduction. (A/B) + (C/D) = E/F */ function bcAdd_Frac ($IntAStr,$IntBStr, $IntCStr,$IntDStr) { $A = trim($IntAStr); $B = trim($IntBStr); $C = trim($IntCStr); $D = trim($IntDStr); $E = bcadd(bcmul($A,$D), bcmul($B,$C)); $F = bcmul($B,$D); return "$E/$F"; } Fraction Subtraction Rule: /* This PHP function performs arbitrary precision integer fraction subtraction without reduction. (A/B)  (C/D) = E/F */ function bcSub_Frac ($IntAStr,$IntBStr, $IntCStr,$IntDStr) { $A = trim($IntAStr); $B = trim($IntBStr); $C = trim($IntCStr); $D = trim($IntDStr); $E = bcsub(bcmul($A,$D), bcmul($B,$C)); $F = bcmul($B,$D); return "$E/$F"; } Fraction Multiplication Rule: /* This PHP function performs arbitrary precision integer fraction multiplication without reduction. (A/B) * (C/D) = E/F */ function bcMul_Frac ($IntAStr,$IntBStr, $IntCStr,$IntDStr) { $A = trim($IntAStr); $B = trim($IntBStr); $C = trim($IntCStr); $D = trim($IntDStr); $E = bcmul($A,$C); $F = bcmul($B,$D); return "$E/$F"; } Fraction Division Rule: /* This function performs arbitrary precision integer fraction division without reduction. (A/B) / (C/D) = (E/F) */ function bcDiv_Frac ($IntAStr,$IntBStr, $IntCStr,$IntDStr) { $A = trim($IntAStr); $B = trim($IntBStr); $C = trim($IntCStr); $D = trim($IntDStr); $E = bcmul($A,$D); $F = bcmul($B,$C); return "$E/$F"; } GCD (Greatest Common Divisor): // This PHP function computes the GCD (Greatest Common Divisor) of any // two integers (±E, ±F). The GCD value will always equate to a // positive integer not less than 1. When GCD=1, it means that // the given fraction cannot be reduced and it is already in // its lowest possible integer terms. function bcGCD ($IntEStr, $IntFStr) { $E = bcmul(((bccomp($IntEStr, 0) < 0)? 1:1), $IntEStr); $F = bcmul(((bccomp($IntFStr, 0) < 0)? 1:1), $IntFStr); if (bccomp($E,$F) > 0) {$w=$E; $E=$F; $F=$w;} $GCD = $E; while (bccomp($F, "0") != 0) { $w=$F; $F=bcmod($GCD, $F); $GCD=$w; } return $GCD; } © Jay Tanner  2012
