diff --git a/benchmark/Calculator/BcMathCalculatorBench.php b/benchmark/Calculator/BcMathCalculatorBench.php new file mode 100644 index 00000000..cbb80ded --- /dev/null +++ b/benchmark/Calculator/BcMathCalculatorBench.php @@ -0,0 +1,15 @@ + + */ + abstract protected function getCalculator(): string; + + public function benchCompare(): void + { + $this->getCalculator()::compare('1', '1'); + $this->getCalculator()::compare('1', '5'); + $this->getCalculator()::compare('5', '5'); + $this->getCalculator()::compare('5.5', '1.5'); + $this->getCalculator()::compare('1.5', '5.5'); + } + + public function benchAdd(): void + { + $this->getCalculator()::add('1', '5'); + } + + public function benchSubtract(): void + { + $this->getCalculator()::subtract('1', '5'); + } + + public function benchMultiply(): void + { + $this->getCalculator()::multiply('5', '25'); + $this->getCalculator()::multiply('5', '1.5'); + } + + public function benchDivide(): void + { + $this->getCalculator()::divide('5', '4'); + } + + public function benchCeil(): void + { + $this->getCalculator()::ceil('5.5'); + } + + public function benchFloor(): void + { + $this->getCalculator()::floor('5.5'); + } + + public function benchAbsolute(): void + { + $this->getCalculator()::absolute('5'); + $this->getCalculator()::absolute('-5'); + } + + public function benchRound(): void + { + $this->getCalculator()::round('2.6', Money::ROUND_HALF_EVEN); + } + + public function benchShare(): void + { + $this->getCalculator()::share('10', '2', '4'); + } + + public function benchMod(): void + { + $this->getCalculator()::mod('11', '5'); + } +} diff --git a/benchmark/Calculator/GmpCalculatorBench.php b/benchmark/Calculator/GmpCalculatorBench.php new file mode 100644 index 00000000..a026f28c --- /dev/null +++ b/benchmark/Calculator/GmpCalculatorBench.php @@ -0,0 +1,15 @@ +a, $this->b, $this->a, $this->b); + Money::max($this->a, $this->b, $this->a, $this->b); } public function benchAvg(): void { - Money::min($this->a, $this->b, $this->a, $this->b); + Money::avg($this->a, $this->b, $this->a, $this->b); } public function benchRatioOf(): void diff --git a/src/Calculator/BcMathCalculator.php b/src/Calculator/BcMathCalculator.php index 0640c883..087b3c05 100644 --- a/src/Calculator/BcMathCalculator.php +++ b/src/Calculator/BcMathCalculator.php @@ -17,6 +17,7 @@ use function bcmul; use function bcsub; use function ltrim; +use function str_contains; final class BcMathCalculator implements Calculator { @@ -31,13 +32,17 @@ public static function compare(string $a, string $b): int /** @psalm-pure */ public static function add(string $amount, string $addend): string { - return bcadd($amount, $addend, self::SCALE); + $scale = str_contains($amount . $addend, '.') ? self::SCALE : 0; + + return bcadd($amount, $addend, $scale); } /** @psalm-pure */ public static function subtract(string $amount, string $subtrahend): string { - return bcsub($amount, $subtrahend, self::SCALE); + $scale = str_contains($amount . $subtrahend, '.') ? self::SCALE : 0; + + return bcsub($amount, $subtrahend, $scale); } /** @psalm-pure */