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