public function Fraction::reduce in Fraction 8
Same name and namespace in other branches
- 2.x src/Fraction.php \Drupal\fraction\Fraction::reduce()
Reduces the fraction to its simplest form.
Return value
Fraction Returns this Fraction object.
Overrides FractionInterface::reduce
File
- src/
Fraction.php, line 223
Class
- Fraction
- A simple class for representing and acting upon a fraction.
Namespace
Drupal\fractionCode
public function reduce() {
// Get the numerator and denominator.
$numerator = $this
->getNumerator();
$denominator = $this
->getDenominator();
// Calculate the greatest common divisor.
$gcd = $this
->gcd();
// Divide the numerator and denominator by the gcd.
// Use BCMath division if available.
if (function_exists('bcdiv')) {
$numerator = bcdiv($numerator, $gcd, 0);
$denominator = bcdiv($denominator, $gcd, 0);
}
else {
$numerator = $numerator / $gcd;
$denominator = $denominator / $gcd;
}
// Save the numerator and denominator.
$this
->setNumerator($numerator);
$this
->setDenominator($denominator);
return $this;
}