Example #1
0
 /**
  * Returns the result of the division of this number by the given one, at the given scale.
  *
  * @param \Arki\Math\Number|int|float|string $that         The divisor.
  * @param int|null                           $scale        The desired scale, or null to use the scale of this
  *                                                         number.
  * @param int                                $roundingMode An optional rounding mode.
  *
  * @return BigDecimal
  *
  * @throws \ArithmeticError     If the number is invalid or rounding was necessary.
  * @throws \DivisionByZeroError If the number is zero
  * @throws \InvalidArgumentException If the scale is negative
  */
 public function dividedBy($that, $scale = null, $roundingMode = RoundingMode::UNNECESSARY)
 {
     $that = self::of($that);
     if ($that->isZero()) {
         throw new \DivisionByZeroError();
     }
     if ($scale === null) {
         $scale = $this->scale;
     } else {
         $scale = (int) $scale;
         if ($scale < 0) {
             throw new \InvalidArgumentException('Scale cannot be negative.');
         }
     }
     if ($that->intVal->isEqualTo('1') && $that->scale === 0 && $scale === $this->scale) {
         return $this;
     }
     $p = $this->valueWithMinScale($that->scale + $scale);
     $q = $that->valueWithMinScale($this->scale - $scale);
     $result = Math::divRound($p, $q, $roundingMode);
     return new self(BigInteger::parse($result), $scale);
 }