2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.math.util;
20
import java.io.Serializable;
21
import java.math.BigDecimal;
22
import java.math.BigInteger;
23
import java.math.MathContext;
25
import org.apache.commons.math.Field;
26
import org.apache.commons.math.FieldElement;
29
* Arbitrary precision decimal number.
31
* This class is a simple wrapper around the standard <code>BigDecimal</code>
32
* in order to implement the {@link FieldElement} interface.
35
* @version $Revision: 795963 $ $Date: 2009-07-20 15:23:43 -0400 (Mon, 20 Jul 2009) $
37
public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {
39
/** Serializable version identifier. */
40
private static final long serialVersionUID = 7887631840434052850L;
42
/** A big real representing 0. */
43
public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);
45
/** A big real representing 1. */
46
public static final BigReal ONE = new BigReal(BigDecimal.ONE);
48
/** Underlying BigDecimal. */
49
private final BigDecimal d;
51
/** Build an instance from a BigDecimal.
52
* @param val value of the instance
54
public BigReal(BigDecimal val) {
58
/** Build an instance from a BigInteger.
59
* @param val value of the instance
61
public BigReal(BigInteger val) {
62
d = new BigDecimal(val);
65
/** Build an instance from an unscaled BigInteger.
66
* @param unscaledVal unscaled value
67
* @param scale scale to use
69
public BigReal(BigInteger unscaledVal, int scale) {
70
d = new BigDecimal(unscaledVal, scale);
73
/** Build an instance from an unscaled BigInteger.
74
* @param unscaledVal unscaled value
75
* @param scale scale to use
78
public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
79
d = new BigDecimal(unscaledVal, scale, mc);
82
/** Build an instance from a BigInteger.
83
* @param val value of the instance
84
* @param mc context to use
86
public BigReal(BigInteger val, MathContext mc) {
87
d = new BigDecimal(val, mc);
90
/** Build an instance from a characters representation.
91
* @param in character representation of the value
93
public BigReal(char[] in) {
94
d = new BigDecimal(in);
97
/** Build an instance from a characters representation.
98
* @param in character representation of the value
99
* @param offset offset of the first character to analyze
100
* @param len length of the array slice to analyze
102
public BigReal(char[] in, int offset, int len) {
103
d = new BigDecimal(in, offset, len);
106
/** Build an instance from a characters representation.
107
* @param in character representation of the value
108
* @param offset offset of the first character to analyze
109
* @param len length of the array slice to analyze
110
* @param mc context to use
112
public BigReal(char[] in, int offset, int len, MathContext mc) {
113
d = new BigDecimal(in, offset, len, mc);
116
/** Build an instance from a characters representation.
117
* @param in character representation of the value
118
* @param mc context to use
120
public BigReal(char[] in, MathContext mc) {
121
d = new BigDecimal(in, mc);
124
/** Build an instance from a double.
125
* @param val value of the instance
127
public BigReal(double val) {
128
d = new BigDecimal(val);
131
/** Build an instance from a double.
132
* @param val value of the instance
133
* @param mc context to use
135
public BigReal(double val, MathContext mc) {
136
d = new BigDecimal(val, mc);
139
/** Build an instance from an int.
140
* @param val value of the instance
142
public BigReal(int val) {
143
d = new BigDecimal(val);
146
/** Build an instance from an int.
147
* @param val value of the instance
148
* @param mc context to use
150
public BigReal(int val, MathContext mc) {
151
d = new BigDecimal(val, mc);
154
/** Build an instance from a long.
155
* @param val value of the instance
157
public BigReal(long val) {
158
d = new BigDecimal(val);
161
/** Build an instance from a long.
162
* @param val value of the instance
163
* @param mc context to use
165
public BigReal(long val, MathContext mc) {
166
d = new BigDecimal(val, mc);
169
/** Build an instance from a String representation.
170
* @param val character representation of the value
172
public BigReal(String val) {
173
d = new BigDecimal(val);
176
/** Build an instance from a String representation.
177
* @param val character representation of the value
178
* @param mc context to use
180
public BigReal(String val, MathContext mc) {
181
d = new BigDecimal(val, mc);
185
public BigReal add(BigReal a) {
186
return new BigReal(d.add(a.d));
190
public BigReal subtract(BigReal a) {
191
return new BigReal(d.subtract(a.d));
195
public BigReal divide(BigReal a) throws ArithmeticException {
196
return new BigReal(d.divide(a.d));
200
public BigReal multiply(BigReal a) {
201
return new BigReal(d.multiply(a.d));
205
public int compareTo(BigReal a) {
206
return d.compareTo(a.d);
209
/** Get the double value corresponding to the instance.
210
* @return double value corresponding to the instance
212
public double doubleValue() {
213
return d.doubleValue();
216
/** Get the BigDecimal value corresponding to the instance.
217
* @return BigDecimal value corresponding to the instance
219
public BigDecimal bigDecimalValue() {
225
public boolean equals(Object other) {
230
return d.equals(((BigReal) other).d);
231
} catch (ClassCastException cce) {
238
public int hashCode() {
243
public Field<BigReal> getField() {
244
return BigRealField.getInstance();