Arbitrary Precision Fractional Arithmetic Utility
This is a simple arbitrary precision fractional arithmetic utility to perform the basic four arithmetic operations on any two given fractions. The fractional values can be integer and/or decimal values in any combination. The output will be expressed in raw form and also as a reduced integer fraction, when possible.
Below are the PHP functions used in the above program and developed to perform arbitrary precision fractional arithmetic. Arbitrary precision means to virtually any specified number of digits and/or decimals. For example, we could very easily design programs to use numbers with hundreds of decimals in our computations if we wished, such as computing a square root out to 100 or 500 decimals or any other arbitrary precision level, within the resources of the computer.
In the above program, the precision limit is set to about 100 digits for practical reasons, but that should be more than sufficient for all practical purposes and beyond. In order to work with integer fractions, we need to establish the basic rules. Given two integer fractions: and , there are the four basic, standard arithmetic operations. We may add, subtract, multiply or divide The number values may be any combination of negative and positive integers. 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.
Fraction Addition Rule: /* This PHP function performs arbitrary precision integer fraction addition. (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. (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. (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 PHP function performs arbitrary precision integer fraction division. (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"; } The Greatest Common Divisor (GCD) and Reduction of a Fraction: Given two arbitrary precision integer strings (A, B), the following function will return the greatest common divisor (GCD), or the largest integer that will perfectly divide both (A, B) in common. The value of the GCD will range anywhere from 1 to a value no greater than the lesser of the two integers. When GCD=1, it means that the two given integers have no common integer divisor (other than 1, which divides anything). The GCD never equates to zero. If the integers (A, B) are the numerator and denominator of a fraction, then we can reduce a fraction to its lowest terms by dividing them both by the GCD value and using those reduced values in place of the original values. // This PHP function computes the GCD (Greatest Common Divisor) // of any two arbitrary precision integers (±E, ±F). The value // of the GCD will always equate to a positive integer > zero. function bcGCD_EF ($IntEStr, $IntFStr) { $E = trim($IntEStr); $E = bcmul(((bccomp($E,0) < 0)? 1:1), $E); $F = trim($IntFStr); $F = bcmul(((bccomp($F,0) < 0)? 1:1), $F); if (bccomp($E,$F) > 0) {$w=$E; $E=$F; $F=$w;} $GCD = $E; while ($F != 0) { $C=$F; $F=bcmod($GCD, $F); $GCD=$C; } return $GCD; } © Jay tanner  2017
