You are here

public function Fraction::divide in Fraction 8

Same name and namespace in other branches
  1. 2.x src/Fraction.php \Drupal\fraction\Fraction::divide()

Divides this fraction by another one.

Parameters

Fraction $fraction: Another fraction object to divide by.

Return value

Fraction Returns this Fraction object.

Overrides FractionInterface::divide

File

src/Fraction.php, line 356

Class

Fraction
A simple class for representing and acting upon a fraction.

Namespace

Drupal\fraction

Code

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