function NativeCalculator::doDiv
Performs the division of two non-signed large integers.
Return value
string[] The quotient and remainder.
1 call to NativeCalculator::doDiv()
- NativeCalculator::divQR in vendor/
brick/ math/ src/ Internal/ Calculator/ NativeCalculator.php - Returns the quotient and remainder of the division of two numbers.
File
-
vendor/
brick/ math/ src/ Internal/ Calculator/ NativeCalculator.php, line 474
Class
- NativeCalculator
- Calculator implementation using only native PHP code.
Namespace
Brick\Math\Internal\CalculatorCode
private function doDiv(string $a, string $b) : array {
$cmp = $this->doCmp($a, $b);
if ($cmp === -1) {
return [
'0',
$a,
];
}
$x = \strlen($a);
$y = \strlen($b);
// we now know that a >= b && x >= y
$q = '0';
// quotient
$r = $a;
// remainder
$z = $y;
// focus length, always $y or $y+1
for (;;) {
$focus = \substr($a, 0, $z);
$cmp = $this->doCmp($focus, $b);
if ($cmp === -1) {
if ($z === $x) {
// remainder < dividend
break;
}
$z++;
}
$zeros = \str_repeat('0', $x - $z);
$q = $this->add($q, '1' . $zeros);
$a = $this->sub($a, $b . $zeros);
$r = $a;
if ($r === '0') {
// remainder == 0
break;
}
$x = \strlen($a);
if ($x < $y) {
// remainder < dividend
break;
}
$z = $y;
}
return [
$q,
$r,
];
}