Exemple #1
0
 /**
  * @param Decimal $dividend The ratio's dividend.
  * @param Decimal $divisor  The ratio's divisor.
  */
 public function __construct(Decimal $dividend, Decimal $divisor)
 {
     if ($divisor->isZero()) {
         throw new \InvalidArgumentException('Cannot create Ratio with zero consequent.');
     }
     $this->dividend = $dividend;
     $this->divisor = $divisor;
 }
Exemple #2
0
 /**
  * Divides this decimal by another.
  *
  * @param Decimal $other
  * @param int     $scale
  *
  * @return Decimal
  */
 public function divideBy(Decimal $other, int $scale = null) : Decimal
 {
     if ($other->isZero()) {
         throw new \LogicException('Cannot divide by zero.');
     }
     if ($other->isOne()) {
         return $this;
     }
     if (null === $scale) {
         // Determine a reasonable scale to avoid too much loss of precision.
         $abs = $this->abs();
         $otherAbs = $other->abs();
         $otherLog10 = self::computeLog10($otherAbs->value, $otherAbs->numberOfDecimalPlaces(), 1);
         $log10 = self::computeLog10($abs->value, $abs->numberOfDecimalPlaces(), 1) - $otherLog10;
         $totalDecimalPlaces = $this->numberOfDecimalPlaces() + $other->numberOfDecimalPlaces();
         $maxSignificantDigits = max($this->numberOfSignificantDigits(), $other->numberOfSignificantDigits());
         $scale = (int) max($totalDecimalPlaces, $maxSignificantDigits - max(ceil($log10), 0), ceil(-$log10) + 1);
     }
     $result = bcdiv($this->value, $other->value, $scale);
     return static::fromString($result);
 }