You are here

function Fraction::multiply in Fraction 7

Multiply this fraction with another one.

Parameters

Fraction $fraction: Another fraction object to multiply with.

Return value

Fraction Returns this Fraction object.

File

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

Class

Fraction
@file Fraction class

Code

function multiply(Fraction $fraction) {

  // Get the numerator and denominator of each fraction.
  $numerator1 = $this
    ->getNumerator();
  $denominator1 = $this
    ->getDenominator();
  $numerator2 = $fraction
    ->getNumerator();
  $denominator2 = $fraction
    ->getDenominator();

  // Calculate the product 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;
}