BcMathCalculator.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace IAWPSCOPED\Brick\Math\Internal\Calculator; |
| 5 | |
| 6 | use IAWPSCOPED\Brick\Math\Internal\Calculator; |
| 7 | /** |
| 8 | * Calculator implementation built around the bcmath library. |
| 9 | * |
| 10 | * @internal |
| 11 | * |
| 12 | * @psalm-immutable |
| 13 | */ |
| 14 | class BcMathCalculator extends Calculator |
| 15 | { |
| 16 | public function add(string $a, string $b) : string |
| 17 | { |
| 18 | return \bcadd($a, $b, 0); |
| 19 | } |
| 20 | public function sub(string $a, string $b) : string |
| 21 | { |
| 22 | return \bcsub($a, $b, 0); |
| 23 | } |
| 24 | public function mul(string $a, string $b) : string |
| 25 | { |
| 26 | return \bcmul($a, $b, 0); |
| 27 | } |
| 28 | public function divQ(string $a, string $b) : string |
| 29 | { |
| 30 | return \bcdiv($a, $b, 0); |
| 31 | } |
| 32 | /** |
| 33 | * @psalm-suppress InvalidNullableReturnType |
| 34 | * @psalm-suppress NullableReturnStatement |
| 35 | */ |
| 36 | public function divR(string $a, string $b) : string |
| 37 | { |
| 38 | return \bcmod($a, $b, 0); |
| 39 | } |
| 40 | public function divQR(string $a, string $b) : array |
| 41 | { |
| 42 | $q = \bcdiv($a, $b, 0); |
| 43 | $r = \bcmod($a, $b, 0); |
| 44 | \assert($r !== null); |
| 45 | return [$q, $r]; |
| 46 | } |
| 47 | public function pow(string $a, int $e) : string |
| 48 | { |
| 49 | return \bcpow($a, (string) $e, 0); |
| 50 | } |
| 51 | public function modPow(string $base, string $exp, string $mod) : string |
| 52 | { |
| 53 | return \bcpowmod($base, $exp, $mod, 0); |
| 54 | } |
| 55 | /** |
| 56 | * @psalm-suppress InvalidNullableReturnType |
| 57 | * @psalm-suppress NullableReturnStatement |
| 58 | */ |
| 59 | public function sqrt(string $n) : string |
| 60 | { |
| 61 | return \bcsqrt($n, 0); |
| 62 | } |
| 63 | } |
| 64 |