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\ArithmeticProvider;
|
10
|
|
|
use Samsara\Fermat\Provider\BaseConversionProvider;
|
11
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\NumberInterface;
|
12
|
|
|
use Samsara\Fermat\Types\Decimal;
|
13
|
|
|
use Samsara\Fermat\Types\Base\Interfaces\Numbers\DecimalInterface;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
*
|
17
|
|
|
*/
|
18
|
|
|
class ImmutableDecimal extends Decimal
|
19
|
|
|
{
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @param NumberInterface|string|int|float $mod
|
23
|
|
|
* @return DecimalInterface
|
24
|
|
|
* @throws IntegrityConstraint
|
25
|
|
|
* @throws MissingPackage
|
26
|
|
|
*/
|
27
|
9 |
|
public function continuousModulo(NumberInterface|string|int|float $mod): DecimalInterface
|
28
|
|
|
{
|
29
|
|
|
|
30
|
9 |
|
$mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod);
|
31
|
|
|
|
32
|
9 |
|
$scale = ($this->getScale() < $mod->getScale()) ? $mod->getScale() : $this->getScale();
|
33
|
|
|
|
34
|
9 |
|
$newScale = $scale+2;
|
35
|
9 |
|
$thisNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(NumberBase::Ten), $newScale);
|
36
|
|
|
|
37
|
9 |
|
if (is_object($mod) && method_exists($mod, 'truncateToScale')) {
|
38
|
9 |
|
$mod = $mod->truncateToScale($newScale);
|
39
|
|
|
} else {
|
40
|
|
|
$mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newScale);
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
|
44
|
9 |
|
$multiple = $thisNum->divide($mod, $newScale)->floor();
|
|
|
|
|
45
|
|
|
|
46
|
9 |
|
$subtract = $mod->multiply($multiple);
|
47
|
|
|
|
48
|
9 |
|
$remainder = $thisNum->subtract($subtract);
|
49
|
|
|
|
50
|
9 |
|
return $remainder->truncateToScale($this->getScale()-1);
|
|
|
|
|
51
|
|
|
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
/**
|
55
|
|
|
* @param string $value
|
56
|
|
|
* @param int|null $scale
|
57
|
|
|
* @param NumberBase|null $base
|
58
|
|
|
* @param bool $setToNewBase
|
59
|
|
|
* @return ImmutableDecimal
|
60
|
|
|
* @throws IntegrityConstraint
|
61
|
|
|
*/
|
62
|
93 |
|
protected function setValue(string $value, ?int $scale = null, ?NumberBase $base = null, bool $setToNewBase = false): ImmutableDecimal
|
63
|
|
|
{
|
64
|
|
|
//echo '>>START SET VALUE [From: '.debug_backtrace()[1]['function'].' > '.debug_backtrace()[2]['function'].']<<'.PHP_EOL;
|
|
|
|
|
65
|
|
|
//echo 'Input Value: '.$value.PHP_EOL;
|
66
|
|
|
//echo 'Input Base: '.($base ? $base->value : 'null').PHP_EOL;
|
67
|
93 |
|
$imaginary = false;
|
68
|
|
|
|
69
|
93 |
|
$scale = $scale ?? $this->getScale();
|
70
|
|
|
|
71
|
93 |
|
if (str_contains($value, 'i')) {
|
72
|
7 |
|
$value = str_replace('i', '', $value);
|
73
|
7 |
|
$imaginary = true;
|
74
|
|
|
}
|
75
|
|
|
|
76
|
93 |
|
if ((!is_null($base) && $base != NumberBase::Ten)) {
|
77
|
|
|
$value = BaseConversionProvider::convertStringToBaseTen($value, $base);
|
78
|
|
|
}
|
79
|
|
|
|
80
|
93 |
|
if ($setToNewBase) {
|
81
|
|
|
$base = $base ?? NumberBase::Ten;
|
82
|
|
|
} else {
|
83
|
93 |
|
$base = $this->getBase();
|
84
|
|
|
}
|
85
|
|
|
|
86
|
93 |
|
if ($imaginary) {
|
87
|
7 |
|
$value .= 'i';
|
88
|
|
|
}
|
89
|
|
|
|
90
|
|
|
//echo 'Creating Decimal With: V['.$value.'] B['.$base->value.']'.PHP_EOL;
|
|
|
|
|
91
|
|
|
|
92
|
93 |
|
$return = new ImmutableDecimal($value, $scale, $base, true);
|
93
|
|
|
|
94
|
93 |
|
if (isset($this->calcMode)) {
|
95
|
15 |
|
$return->setMode($this->calcMode);
|
|
|
|
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
//echo '>>END SET VALUE<<'.PHP_EOL;
|
|
|
|
|
99
|
|
|
|
100
|
93 |
|
return $return;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.