Completed
Pull Request — dev (#33)
by Jordan
01:55
created

MutableNumber::continuousModulo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Samsara\Fermat\Values;
4
5
use Samsara\Fermat\Types\Number;
6
use Samsara\Fermat\Types\Base\DecimalInterface;
7
use Samsara\Fermat\Types\Base\NumberInterface;
8
use Samsara\Fermat\Numbers;
9
10
class MutableNumber extends Number implements NumberInterface, DecimalInterface
11
{
12
13 1
    public function modulo($mod)
14
    {
15 1
        $oldBase = $this->convertForModification();
16
17 1
        return (new MutableNumber(bcmod($this->getValue(), $mod), $this->getPrecision()))->convertFromModification($oldBase);
18
    }
19
20 1
    public function continuousModulo($mod)
21
    {
22
23 1
        $mod = Numbers::makeOrDont(Numbers::IMMUTABLE, $mod, $this->precision+1);
24 1
        $oldNum = Numbers::make(Numbers::IMMUTABLE, $this->getValue(), $this->precision+1);
25
26 1
        $multiple = $oldNum->divide($mod)->floor();
27
28 1
        $remainder = $oldNum->subtract($mod->multiply($multiple));
29
30 1
        return Numbers::make(Numbers::MUTABLE, $remainder->truncate($this->precision)->getValue());
31
32
    }
33
34
    /**
35
     * @param $value
36
     *
37
     * @return MutableNumber
38
     */
39 2
    protected function setValue($value)
40
    {
41 2
        $this->value = $value;
42
43 2
        return $this;
44
    }
45
46
}