Passed
Branch master (356932)
by Fabrice
02:58
created

MathOpsAbstract::min()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of OpinHelpers.
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/OpinHelpers
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\Math\OpinHelpers;
11
12
/**
13
 * Abstract class MathOpsAbstract
14
 */
15
abstract class MathOpsAbstract extends MathBaseAbstract
16
{
17
    /**
18
     * @param string[] $numbers
19
     *
20
     * @throws \InvalidArgumentException
21
     *
22
     * @return $this
23
     */
24
    public function add(...$numbers)
25
    {
26
        foreach ($numbers as $number) {
27
            $this->number = bcadd($this->number, static::validateInputNumber($number), $this->precision);
28
        }
29
30
        return $this;
31
    }
32
33
    /**
34
     * @param string[] $numbers
35
     *
36
     * @throws \InvalidArgumentException
37
     *
38
     * @return $this
39
     */
40
    public function sub(...$numbers)
41
    {
42
        foreach ($numbers as $number) {
43
            $this->number = bcsub($this->number, static::validateInputNumber($number), $this->precision);
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param string[] $numbers
51
     *
52
     * @throws \InvalidArgumentException
53
     *
54
     * @return $this
55
     */
56
    public function mul(...$numbers)
57
    {
58
        foreach ($numbers as $number) {
59
            $this->number = bcmul($this->number, static::validateInputNumber($number), $this->precision);
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string[] $numbers
67
     *
68
     * @throws \InvalidArgumentException
69
     *
70
     * @return $this
71
     */
72
    public function div(...$numbers)
73
    {
74
        foreach ($numbers as $number) {
75
            $this->number = bcdiv($this->number, static::validateInputNumber($number), $this->precision);
76
        }
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return $this
83
     */
84
    public function sqrt()
85
    {
86
        $this->number = bcsqrt($this->number, $this->precision);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $exponent
93
     *
94
     * @throws \InvalidArgumentException
95
     *
96
     * @return $this
97
     */
98
    public function pow($exponent)
99
    {
100
        $this->number = bcpow($this->number, static::validatePositiveInteger($exponent), $this->precision);
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $modulus
107
     *
108
     * @throws \InvalidArgumentException
109
     *
110
     * @return $this
111
     */
112
    public function mod($modulus)
113
    {
114
        $this->number = bcmod($this->number, static::validatePositiveInteger($modulus));
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param string $exponent
121
     * @param string $modulus
122
     *
123
     * @throws \InvalidArgumentException
124
     *
125
     * @return $this
126
     */
127
    public function powMod($exponent, $modulus)
128
    {
129
        $this->number = bcpowmod($this->number, static::validatePositiveInteger($exponent), static::validatePositiveInteger($modulus));
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param int $precision
136
     *
137
     * @return $this
138
     */
139
    public function round($precision = 0)
140
    {
141
        $precision = max(0, (int) $precision);
142
        if ($this->hasDecimals()) {
143
            if ($this->isPositive()) {
144
                $this->number = bcadd($this->number, '0.' . str_repeat('0', $precision) . '5', $precision);
145
146
                return $this;
147
            }
148
149
            $this->number = bcsub($this->number, '0.' . str_repeat('0', $precision) . '5', $precision);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * @return $this
157
     */
158
    public function ceil()
159
    {
160
        if ($this->hasDecimals()) {
161
            if ($this->isPositive()) {
162
                $this->number = bcadd($this->number, (preg_match('`\.[0]*$`', $this->number) ? '0' : '1'), 0);
163
164
                return $this;
165
            }
166
167
            $this->number = bcsub($this->number, '0', 0);
168
        }
169
170
        return $this;
171
    }
172
173
    /**
174
     * @return $this
175
     */
176
    public function floor()
177
    {
178
        if ($this->hasDecimals()) {
179
            if ($this->isPositive()) {
180
                $this->number = bcadd($this->number, 0, 0);
181
182
                return $this;
183
            }
184
185
            $this->number = bcsub($this->number, (preg_match('`\.[0]*$`', $this->number) ? '0' : '1'), 0);
186
        }
187
188
        return $this;
189
    }
190
191
    /**
192
     * @return $this
193
     */
194
    public function abs()
195
    {
196
        $this->number = ltrim($this->number, '-');
197
198
        return $this;
199
    }
200
201
    /**
202
     * returns the highest number among all arguments
203
     *
204
     * @param string[] $numbers
205
     *
206
     * @throws \InvalidArgumentException
207
     *
208
     * @return $this
209
     */
210
    public function max(...$numbers)
211
    {
212
        foreach ($numbers as $number) {
213
            if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === 1) {
214
                $this->number = $number;
215
            }
216
        }
217
218
        return $this;
219
    }
220
221
    /**
222
     * returns the smallest number among all arguments
223
     *
224
     * @param string[] $numbers
225
     *
226
     * @throws \InvalidArgumentException
227
     *
228
     * @return $this
229
     */
230
    public function min(...$numbers)
231
    {
232
        foreach ($numbers as $number) {
233
            if (bccomp(static::validateInputNumber($number), $this->number, $this->precision) === -1) {
234
                $this->number = $number;
235
            }
236
        }
237
238
        return $this;
239
    }
240
}
241