|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Values;
|
|
4
|
|
|
|
|
5
|
|
|
use Samsara\Fermat\Numbers;
|
|
6
|
|
|
use Samsara\Fermat\Types\Number;
|
|
7
|
|
|
use Samsara\Fermat\Types\Base\DecimalInterface;
|
|
8
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface;
|
|
9
|
|
|
|
|
10
|
|
|
class ImmutableNumber extends Number implements NumberInterface, DecimalInterface
|
|
11
|
|
|
{
|
|
12
|
|
|
|
|
13
|
5 |
|
public function modulo($mod)
|
|
14
|
|
|
{
|
|
15
|
5 |
|
$oldBase = $this->convertForModification();
|
|
16
|
|
|
|
|
17
|
5 |
|
return (new ImmutableNumber(bcmod($this->getValue(), $mod), $this->getPrecision(), $this->getBase()))->convertFromModification($oldBase);
|
|
18
|
|
|
}
|
|
19
|
|
|
|
|
20
|
7 |
|
public function continuousModulo($mod)
|
|
21
|
|
|
{
|
|
22
|
|
|
|
|
23
|
7 |
|
if (is_object($mod) && method_exists($mod, 'getPrecision')) {
|
|
24
|
6 |
|
$precision = ($this->getPrecision() < $mod->getPrecision()) ? $mod->getPrecision() : $this->getPrecision();
|
|
25
|
|
|
} else {
|
|
26
|
1 |
|
$precision = $this->getPrecision();
|
|
27
|
|
|
}
|
|
28
|
|
|
|
|
29
|
7 |
|
$oldPrecision = $this->precision;
|
|
30
|
7 |
|
$newPrecision = $precision+1;
|
|
31
|
|
|
|
|
32
|
7 |
|
$this->precision = $newPrecision;
|
|
33
|
|
|
|
|
34
|
7 |
|
if (is_object($mod) && method_exists($mod, 'truncateToPrecision')) {
|
|
35
|
6 |
|
$mod = $mod->truncateToPrecision($newPrecision);
|
|
36
|
|
|
} else {
|
|
37
|
1 |
|
$mod = Numbers::make(Numbers::IMMUTABLE, $mod, $newPrecision);
|
|
38
|
|
|
}
|
|
39
|
|
|
|
|
40
|
7 |
|
$multiple = $this->divide($mod)->floor();
|
|
41
|
|
|
|
|
42
|
7 |
|
$remainder = $this->subtract($mod->multiply($multiple));
|
|
43
|
|
|
|
|
44
|
7 |
|
$this->precision = $oldPrecision;
|
|
45
|
|
|
|
|
46
|
7 |
|
return $remainder->truncateToPrecision($oldPrecision);
|
|
47
|
|
|
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
/**
|
|
51
|
|
|
* @return bool
|
|
52
|
|
|
*/
|
|
53
|
|
|
public function isComplex(): bool
|
|
54
|
|
|
{
|
|
55
|
69 |
|
return false;
|
|
56
|
|
|
}
|
|
57
|
69 |
|
|
|
58
|
|
|
/**
|
|
59
|
|
|
* @param $value
|
|
60
|
|
|
*
|
|
61
|
|
|
* @return ImmutableNumber
|
|
62
|
|
|
*/
|
|
63
|
|
|
protected function setValue($value)
|
|
64
|
|
|
{
|
|
65
|
|
|
return new ImmutableNumber($value, $this->getPrecision(), $this->getBase());
|
|
66
|
|
|
}
|
|
67
|
|
|
|
|
68
|
|
|
} |