1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.linear;
18
19
20 import java.io.Serializable;
21 import java.math.BigDecimal;
22 import java.math.BigInteger;
23 import java.math.MathContext;
24 import java.math.RoundingMode;
25
26 import org.apache.commons.math4.legacy.core.Field;
27 import org.apache.commons.math4.legacy.core.FieldElement;
28 import org.apache.commons.math4.legacy.exception.MathArithmeticException;
29 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
30
31
32
33
34
35
36
37
38
39 public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {
40
41
42 public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);
43
44
45 public static final BigReal ONE = new BigReal(BigDecimal.ONE);
46
47
48 private static final long serialVersionUID = 4984534880991310382L;
49
50
51 private final BigDecimal d;
52
53
54 private RoundingMode roundingMode = RoundingMode.HALF_UP;
55
56
57 private int scale = 64;
58
59
60
61
62 public BigReal(BigDecimal val) {
63 d = val;
64 }
65
66
67
68
69 public BigReal(BigInteger val) {
70 d = new BigDecimal(val);
71 }
72
73
74
75
76
77 public BigReal(BigInteger unscaledVal, int scale) {
78 d = new BigDecimal(unscaledVal, scale);
79 }
80
81
82
83
84
85
86 public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
87 d = new BigDecimal(unscaledVal, scale, mc);
88 }
89
90
91
92
93
94 public BigReal(BigInteger val, MathContext mc) {
95 d = new BigDecimal(val, mc);
96 }
97
98
99
100
101 public BigReal(char[] in) {
102 d = new BigDecimal(in);
103 }
104
105
106
107
108
109
110 public BigReal(char[] in, int offset, int len) {
111 d = new BigDecimal(in, offset, len);
112 }
113
114
115
116
117
118
119
120 public BigReal(char[] in, int offset, int len, MathContext mc) {
121 d = new BigDecimal(in, offset, len, mc);
122 }
123
124
125
126
127
128 public BigReal(char[] in, MathContext mc) {
129 d = new BigDecimal(in, mc);
130 }
131
132
133
134
135 public BigReal(double val) {
136 d = new BigDecimal(val);
137 }
138
139
140
141
142
143 public BigReal(double val, MathContext mc) {
144 d = new BigDecimal(val, mc);
145 }
146
147
148
149
150 public BigReal(int val) {
151 d = new BigDecimal(val);
152 }
153
154
155
156
157
158 public BigReal(int val, MathContext mc) {
159 d = new BigDecimal(val, mc);
160 }
161
162
163
164
165 public BigReal(long val) {
166 d = new BigDecimal(val);
167 }
168
169
170
171
172
173 public BigReal(long val, MathContext mc) {
174 d = new BigDecimal(val, mc);
175 }
176
177
178
179
180 public BigReal(String val) {
181 d = new BigDecimal(val);
182 }
183
184
185
186
187
188 public BigReal(String val, MathContext mc) {
189 d = new BigDecimal(val, mc);
190 }
191
192
193
194
195
196
197
198 public RoundingMode getRoundingMode() {
199 return roundingMode;
200 }
201
202
203
204
205
206
207 public void setRoundingMode(RoundingMode roundingMode) {
208 this.roundingMode = roundingMode;
209 }
210
211
212
213
214
215
216
217 public int getScale() {
218 return scale;
219 }
220
221
222
223
224
225
226 public void setScale(int scale) {
227 this.scale = scale;
228 }
229
230
231 @Override
232 public BigReal add(BigReal a) {
233 return new BigReal(d.add(a.d));
234 }
235
236
237 @Override
238 public BigReal subtract(BigReal a) {
239 return new BigReal(d.subtract(a.d));
240 }
241
242
243 @Override
244 public BigReal negate() {
245 return new BigReal(d.negate());
246 }
247
248
249
250
251
252
253 @Override
254 public BigReal divide(BigReal a) throws MathArithmeticException {
255 try {
256 return new BigReal(d.divide(a.d, scale, roundingMode));
257 } catch (ArithmeticException e) {
258
259 throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
260 }
261 }
262
263
264
265
266
267
268 @Override
269 public BigReal reciprocal() throws MathArithmeticException {
270 try {
271 return new BigReal(BigDecimal.ONE.divide(d, scale, roundingMode));
272 } catch (ArithmeticException e) {
273
274 throw new MathArithmeticException(LocalizedFormats.ZERO_NOT_ALLOWED);
275 }
276 }
277
278
279 @Override
280 public BigReal multiply(BigReal a) {
281 return new BigReal(d.multiply(a.d));
282 }
283
284
285 @Override
286 public BigReal multiply(final int n) {
287 return new BigReal(d.multiply(new BigDecimal(n)));
288 }
289
290
291 @Override
292 public int compareTo(BigReal a) {
293 return d.compareTo(a.d);
294 }
295
296
297
298
299 public double doubleValue() {
300 return d.doubleValue();
301 }
302
303
304
305
306 public BigDecimal bigDecimalValue() {
307 return d;
308 }
309
310
311 @Override
312 public boolean equals(Object other) {
313 if (this == other){
314 return true;
315 }
316
317 if (other instanceof BigReal) {
318 return d.compareTo(((BigReal) other).d) == 0;
319 }
320 return false;
321 }
322
323
324 @Override
325 public int hashCode() {
326 return Double.hashCode(d.doubleValue());
327 }
328
329
330 @Override
331 public Field<BigReal> getField() {
332 return BigRealField.getInstance();
333 }
334 }