Exemplo n.º 1
0
 /**
  * @return Ratio
  */
 public function clearDecimals() : Ratio
 {
     if (!$this->containsDecimalPoint()) {
         return $this;
     }
     $decimalPlaces = max($this->dividend->numberOfDecimalPlaces(), $this->divisor->numberOfDecimalPlaces());
     $multiplier = bcpow(10, $decimalPlaces);
     $antecedent = bcmul($this->dividend->toString(), $multiplier, 0);
     $consequent = bcmul($this->divisor->toString(), $multiplier, 0);
     return new static(new Decimal($antecedent), new Decimal($consequent));
 }
Exemplo n.º 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);
 }