function NativeCalculator::divQR
Overrides Calculator::divQR
File
-
vendor/
brick/ math/ src/ Internal/ Calculator/ NativeCalculator.php, line 129
Class
- NativeCalculator
- Calculator implementation using only native PHP code.
Namespace
Brick\Math\Internal\CalculatorCode
public function divQR(string $a, string $b) : array {
if ($a === '0') {
return [
'0',
'0',
];
}
if ($a === $b) {
return [
'1',
'0',
];
}
if ($b === '1') {
return [
$a,
'0',
];
}
if ($b === '-1') {
return [
$this->neg($a),
'0',
];
}
/** @psalm-var numeric-string $a */
$na = $a * 1;
// cast to number
if (is_int($na)) {
/** @psalm-var numeric-string $b */
$nb = $b * 1;
if (is_int($nb)) {
// the only division that may overflow is PHP_INT_MIN / -1,
// which cannot happen here as we've already handled a divisor of -1 above.
$q = intdiv($na, $nb);
$r = $na % $nb;
return [
(string) $q,
(string) $r,
];
}
}
[
$aNeg,
$bNeg,
$aDig,
$bDig,
] = $this->init($a, $b);
[
$q,
$r,
] = $this->doDiv($aDig, $bDig);
if ($aNeg !== $bNeg) {
$q = $this->neg($q);
}
if ($aNeg) {
$r = $this->neg($r);
}
return [
$q,
$r,
];
}