|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\System; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Decimal Class. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Decimal extends Real |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected static $defaultScale = 12; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param int $scale |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \InvalidArgumentException |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function setDefaultScale($scale) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!is_int($scale) || $scale < 0) { |
|
34
|
|
|
throw new \InvalidArgumentException('The scale must be a positive integer'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
self::$defaultScale = $scale; |
|
38
|
|
|
\bcscale(self::$defaultScale); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return int |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function defaultScale() |
|
45
|
|
|
{ |
|
46
|
|
|
return self::$defaultScale; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param float|int|string $value |
|
51
|
|
|
* |
|
52
|
|
|
* @return Decimal |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function fromNative($value) |
|
55
|
|
|
{ |
|
56
|
|
|
if (\is_infinite((float) $value)) { |
|
57
|
|
|
return DecimalInfinite::fromNative($value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return new self($value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $scale |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
protected function scale($scale) |
|
69
|
|
|
{ |
|
70
|
|
|
if ($scale !== null && (!is_int($scale) || $scale < 0)) { |
|
71
|
|
|
throw new \InvalidArgumentException('The scale must be a positive integer'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $scale === null ? self::$defaultScale : $scale; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function invertedAdd(Number $x) |
|
81
|
|
|
{ |
|
82
|
|
|
return $x->addDecimal($this); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
|
|
public function addInteger(Integer $x) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->addDecimal($x->toDecimal()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
|
|
public function addReal(Real $x) |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->addDecimal($x->toDecimal()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function addDecimal(Decimal $x, $scale = null) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($x->isInfinite()) { |
|
107
|
|
|
return $x; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return new self(\bcadd($this->toNative(), $x->toNative(), $this->scale($scale))); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function invertedSub(Number $x) |
|
117
|
|
|
{ |
|
118
|
|
|
return $x->subDecimal($this); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function subInteger(Integer $x) |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->subDecimal($x->toDecimal()); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function subReal(Real $x) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->subDecimal($x->toDecimal()); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function subDecimal(Decimal $x, $scale = null) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($x->isInfinite()) { |
|
143
|
|
|
return $x->isPositive() ? DecimalInfinite::infNegative() : DecimalInfinite::infPositive(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return new self(\bcsub($this->toNative(), $x->toNative(), $this->scale($scale))); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function invertedMult(Number $x) |
|
153
|
|
|
{ |
|
154
|
|
|
return $x->multDecimal($this); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function multInteger(Integer $x) |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->multDecimal($x->toDecimal()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* {@inheritdoc} |
|
167
|
|
|
*/ |
|
168
|
|
|
public function multReal(Real $x) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->multDecimal($x->toDecimal()); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* {@inheritdoc} |
|
175
|
|
|
*/ |
|
176
|
|
|
public function multDecimal(Decimal $x, $scale = null) |
|
177
|
|
|
{ |
|
178
|
|
|
if ($x->isInfinite()) { |
|
179
|
|
|
return $x->multDecimal($this); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return new self(\bcmul($this->toNative(), $x->toNative(), $this->scale($scale))); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* {@inheritdoc} |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function invertedDiv(Number $x) |
|
189
|
|
|
{ |
|
190
|
|
|
return $x->divDecimal($this); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritdoc} |
|
195
|
|
|
*/ |
|
196
|
|
|
public function divInteger(Integer $x) |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->divDecimal($x->toDecimal()); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* {@inheritdoc} |
|
203
|
|
|
*/ |
|
204
|
|
|
public function divReal(Real $x) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->divDecimal($x->toDecimal()); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* {@inheritdoc} |
|
211
|
|
|
*/ |
|
212
|
|
|
public function divDecimal(Decimal $x, $scale = null) |
|
213
|
|
|
{ |
|
214
|
|
|
$value = $this->divSpecialCases($x); |
|
215
|
|
|
if ($value !== null) { |
|
216
|
|
|
return $value; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return new self(\bcdiv($this->toNative(), $x->toNative(), $this->scale($scale))); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* {@inheritdoc} |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function invertedPow(Number $x) |
|
226
|
|
|
{ |
|
227
|
|
|
return $x->powDecimal($this); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* {@inheritdoc} |
|
232
|
|
|
*/ |
|
233
|
|
|
public function powInteger(Integer $x) |
|
234
|
|
|
{ |
|
235
|
|
|
return new self(\bcpow($this->toNative(), $x->toNative(), $this->scale(null))); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* {@inheritdoc} |
|
240
|
|
|
*/ |
|
241
|
|
|
public function sqrt($scale = null) |
|
242
|
|
|
{ |
|
243
|
|
|
return new self(\bcsqrt($this->toNative(), $this->scale($scale))); |
|
244
|
|
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|