Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 092833d

Browse files
committed
Simplified Decimal::div method
1 parent ae415b6 commit 092833d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/Decimal.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,11 @@ public function div(Decimal $b, $scale = null)
380380
$divscale = $scale + 1;
381381
} else {
382382
// $divscale is calculated in order to maintain a reasonable precision
383-
$one = Decimal::fromInteger(1);
384383
$this_abs = $this->abs();
385384
$b_abs = $b->abs();
386385

387-
$this_significative_digits = strlen($this->value) - (
388-
($this_abs->comp($one) === -1) ? 2 : ($this->scale > 0 ? 1 : 0)
389-
) - ($this->isNegative() ? 1 : 0);
390-
391-
$b_significative_digits = strlen($b->value) - (
392-
($b_abs->comp($one) === -1) ? 2 : ($b->scale > 0 ? 1 : 0)
393-
) - ($b->isNegative() ? 1 : 0);
386+
$this_significative_digits = self::countSignificativeDigits($this, $this_abs);
387+
$b_significative_digits = self::countSignificativeDigits($b, $b_abs);
394388

395389
$log10_result =
396390
self::innerLog10($this_abs->value, $this_abs->scale, 1) -
@@ -830,6 +824,22 @@ private static function normalizeSign($sign)
830824
return $sign;
831825
}
832826

827+
/**
828+
* Counts the number of significative digits of $val
829+
*
830+
* @param Decimal $val
831+
* @param Decimal $abs $val->abs()
832+
* @return integer
833+
*/
834+
private static function countSignificativeDigits(Decimal $val, Decimal $abs)
835+
{
836+
$one = Decimal::fromInteger(1);
837+
838+
return strlen($val->value) - (
839+
($abs->comp($one) === -1) ? 2 : max($val->scale, 1)
840+
) - ($val->isNegative() ? 1 : 0);
841+
}
842+
833843
/**
834844
* @return string
835845
*/

0 commit comments

Comments
 (0)