1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values;
|
4
|
|
|
|
5
|
|
|
use Samsara\Exceptions\SystemError\PlatformError\MissingPackage;
|
6
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint;
|
7
|
|
|
use Samsara\Fermat\Enums\NumberBase;
|
8
|
|
|
use Samsara\Fermat\Numbers;
|
9
|
|
|
use Samsara\Fermat\Provider\BaseConversionProvider;
|
10
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
|
11
|
|
|
use Samsara\Fermat\Types\Decimal;
|
12
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
*
|
16
|
|
|
*/
|
17
|
|
|
class ImmutableDecimal extends Decimal
|
18
|
|
|
{
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* @param NumberInterface|string|int|float $mod
|
22
|
|
|
* @return DecimalInterface
|
23
|
|
|
* @throws IntegrityConstraint
|
24
|
|
|
* @throws MissingPackage
|
25
|
|
|
*/
|
26
|
9 |
|
public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
|
27
|
|
|
{
|
28
|
|
|
|
29
|
9 |
|
if (is_object($mod) && method_exists($mod, 'getScale')) {
|
30
|
6 |
|
$scale = ($this->getScale() < $mod->getScale()) ? $mod->getScale() : $this->getScale();
|
31
|
|
|
} else {
|
32
|
3 |
|
$scale = $this->getScale();
|
33
|
|
|
}
|
34
|
|
|
|
35
|
9 |
|
$oldScale = $this->scale;
|
36
|
9 |
|
$newScale = $scale+1;
|
37
|
|
|
|
38
|
9 |
|
$this->scale = $newScale;
|
39
|
|
|
|
40
|
9 |
|
if (is_object($mod) && method_exists($mod, 'truncateToScale')) {
|
41
|
6 |
|
$mod = $mod->truncateToScale($newScale);
|
42
|
|
|
} else {
|
43
|
3 |
|
$mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newScale);
|
44
|
|
|
}
|
45
|
|
|
|
46
|
9 |
|
$multiple = $this->divide($mod)->floor();
|
|
|
|
|
47
|
|
|
|
48
|
9 |
|
$remainder = $this->subtract($mod->multiply($multiple));
|
49
|
|
|
|
50
|
9 |
|
$this->scale = $oldScale;
|
51
|
|
|
|
52
|
9 |
|
return $remainder->truncateToScale($oldScale);
|
|
|
|
|
53
|
|
|
|
54
|
|
|
}
|
55
|
|
|
|
56
|
|
|
/**
|
57
|
|
|
* @param string $value
|
58
|
|
|
* @param int|null $scale
|
59
|
|
|
* @param NumberBase|null $base
|
60
|
|
|
* @param bool $setToNewBase
|
61
|
|
|
* @return ImmutableDecimal
|
62
|
|
|
* @throws IntegrityConstraint
|
63
|
|
|
*/
|
64
|
91 |
|
protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): ImmutableDecimal
|
65
|
|
|
{
|
66
|
|
|
//echo '>>START SET VALUE [From: '.debug_backtrace()[1]['function'].' > '.debug_backtrace()[2]['function'].']<<'.PHP_EOL;
|
|
|
|
|
67
|
|
|
//echo 'Input Value: '.$value.PHP_EOL;
|
68
|
|
|
//echo 'Input Base: '.($base ? $base->value : 'null').PHP_EOL;
|
69
|
91 |
|
$imaginary = false;
|
70
|
|
|
|
71
|
91 |
|
$scale = $scale ?? $this->getScale();
|
72
|
|
|
|
73
|
91 |
|
if (str_contains($value, 'i')) {
|
74
|
7 |
|
$value = str_replace('i', '', $value);
|
75
|
7 |
|
$imaginary = true;
|
76
|
|
|
}
|
77
|
|
|
|
78
|
91 |
|
if ((!is_null($base) && $base != NumberBase::Ten)) {
|
79
|
|
|
$value = BaseConversionProvider::convertStringToBaseTen($value, $base);
|
80
|
|
|
}
|
81
|
|
|
|
82
|
91 |
|
if ($setToNewBase) {
|
83
|
|
|
$base = $base ?? NumberBase::Ten;
|
84
|
|
|
} else {
|
85
|
91 |
|
$base = $this->getBase();
|
86
|
|
|
}
|
87
|
|
|
|
88
|
91 |
|
if ($imaginary) {
|
89
|
7 |
|
$value .= 'i';
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
//echo 'Creating Decimal With: V['.$value.'] B['.$base->value.']'.PHP_EOL;
|
|
|
|
|
93
|
|
|
|
94
|
91 |
|
$return = new ImmutableDecimal($value, $scale, $base, true);
|
95
|
|
|
|
96
|
91 |
|
if (isset($this->calcMode)) {
|
97
|
16 |
|
$return->setMode($this->calcMode);
|
|
|
|
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
//echo '>>END SET VALUE<<'.PHP_EOL;
|
|
|
|
|
101
|
|
|
|
102
|
91 |
|
return $return;
|
103
|
|
|
}
|
104
|
|
|
|
105
|
|
|
} |