Ejemplo n.º 1
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);
 }