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

Commit b9d7948

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

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/Decimal.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -377,32 +377,29 @@ public function div(Decimal $b, $scale = null)
377377
return Decimal::fromInteger(0, $scale);
378378
} else {
379379
if ($scale !== null) {
380-
$divscale = $scale + 1;
380+
$divscale = $scale;
381381
} else {
382382
// $divscale is calculated in order to maintain a reasonable precision
383383
$this_abs = $this->abs();
384384
$b_abs = $b->abs();
385385

386-
$this_significative_digits = self::countSignificativeDigits($this, $this_abs);
387-
$b_significative_digits = self::countSignificativeDigits($b, $b_abs);
388-
389386
$log10_result =
390387
self::innerLog10($this_abs->value, $this_abs->scale, 1) -
391388
self::innerLog10($b_abs->value, $b_abs->scale, 1);
392389

393390
$divscale = max(
394391
$this->scale + $b->scale,
395392
max(
396-
$this_significative_digits,
397-
$b_significative_digits
398-
) - ($log10_result >= 0 ? intval(ceil($log10_result)) : 0),
399-
($log10_result < 0 ? intval(ceil(-$log10_result)) : 0)
400-
) + 1;
393+
self::countSignificativeDigits($this, $this_abs),
394+
self::countSignificativeDigits($b, $b_abs)
395+
) - max(ceil($log10_result), 0),
396+
ceil(-$log10_result)
397+
);
401398
}
402399

403400
return self::fromString(
404-
bcdiv($this->value, $b->value, $divscale),
405-
$divscale-1
401+
bcdiv($this->value, $b->value, $divscale+1),
402+
$divscale
406403
);
407404
}
408405
}

0 commit comments

Comments
 (0)