function Fraction::divide in Fraction 7
Divide this fraction by another one.
Parameters
Fraction $fraction: Another fraction object to divide by.
Return value
Fraction Returns this Fraction object.
File
- ./
fraction.class.inc, line 418 - Fraction class
Class
- Fraction
- @file Fraction class
Code
function divide(Fraction $fraction) {
// Reciprocate the fraction.
$fraction
->reciprocate();
// Get the numerator and denominator of each fraction.
$numerator1 = $this
->getNumerator();
$denominator1 = $this
->getDenominator();
$numerator2 = $fraction
->getNumerator();
$denominator2 = $fraction
->getDenominator();
// Calculate the quotient of the two fractions.
// Use BCMath if available.
if (function_exists('bcmul')) {
$numerator = bcmul($numerator1, $numerator2, 0);
$denominator = bcmul($denominator1, $denominator2, 0);
}
else {
$numerator = $numerator1 * $numerator2;
$denominator = $denominator1 * $denominator2;
}
// Save the numerator and denominator.
$this
->setNumerator($numerator);
$this
->setDenominator($denominator);
return $this;
}