Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions benchmark/Calculator/BcMathCalculatorBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Benchmark\Money\Calculator;

use Money\Calculator\BcMathCalculator;

class BcMathCalculatorBench extends CalculatorBench
{
protected function getCalculator(): string
{
return BcMathCalculator::class;
}
}
77 changes: 77 additions & 0 deletions benchmark/Calculator/CalculatorBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Benchmark\Money\Calculator;

use Money\Calculator;
use Money\Money;

abstract class CalculatorBench
{
/**
* @psalm-return class-string<Calculator>
*/
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');
}
}
15 changes: 15 additions & 0 deletions benchmark/Calculator/GmpCalculatorBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Benchmark\Money\Calculator;

use Money\Calculator\GmpCalculator;

class GmpCalculatorBench extends CalculatorBench
{
protected function getCalculator(): string
{
return GmpCalculator::class;
}
}
4 changes: 2 additions & 2 deletions benchmark/MoneyOperationBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ public function benchMin(): void

public function benchMax(): void
{
Money::min($this->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
Expand Down
9 changes: 7 additions & 2 deletions src/Calculator/BcMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function bcmul;
use function bcsub;
use function ltrim;
use function str_contains;

final class BcMathCalculator implements Calculator
{
Expand All @@ -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 */
Expand Down