-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtraction.php
More file actions
37 lines (27 loc) · 1.14 KB
/
Subtraction.php
File metadata and controls
37 lines (27 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace TinyBlocks\Math\Internal\Operations\Adapters\Scales;
use TinyBlocks\Math\BigNumber;
use TinyBlocks\Math\Internal\Scale;
final readonly class Subtraction
{
private Scale $minuendScale;
private Scale $subtrahendScale;
public function __construct(private BigNumber $minuend, private BigNumber $subtrahend)
{
$this->minuendScale = Scale::from(value: $this->minuend->getScale());
$this->subtrahendScale = Scale::from(value: $this->subtrahend->getScale());
}
public function applyScale(): Scale
{
if ($this->minuendScale->hasAutomaticScale() && $this->subtrahendScale->hasAutomaticScale()) {
$minuendScale = $this->minuendScale->scaleOf(value: $this->minuend->toString());
$subtrahendScale = $this->minuendScale->scaleOf(value: $this->subtrahend->toString());
if ($minuendScale->equals(other: $subtrahendScale)) {
return $minuendScale;
}
return $minuendScale->greaterScale(other: $subtrahendScale);
}
return $this->minuendScale->greaterScale(other: $this->subtrahendScale);
}
}