You are here

public function Fraction::reduce in Fraction 7

Reduce the fraction to its simplest form.

Return value

Fraction Returns this Fraction object.

File

./fraction.class.inc, line 258
Fraction class

Class

Fraction
@file Fraction class

Code

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;
}