001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.util;
018
019
020 import java.io.Serializable;
021 import java.math.BigDecimal;
022 import java.math.BigInteger;
023 import java.math.MathContext;
024 import java.math.RoundingMode;
025
026 import org.apache.commons.math.Field;
027 import org.apache.commons.math.FieldElement;
028
029 /**
030 * Arbitrary precision decimal number.
031 * <p>
032 * This class is a simple wrapper around the standard <code>BigDecimal</code>
033 * in order to implement the {@link FieldElement} interface.
034 * </p>
035 * @since 2.0
036 * @version $Revision: 925812 $ $Date: 2010-03-21 16:49:31 +0100 (dim. 21 mars 2010) $
037 */
038 public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {
039
040 /** A big real representing 0. */
041 public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);
042
043 /** A big real representing 1. */
044 public static final BigReal ONE = new BigReal(BigDecimal.ONE);
045
046 /** Serializable version identifier. */
047 private static final long serialVersionUID = 4984534880991310382L;
048
049 /** Underlying BigDecimal. */
050 private final BigDecimal d;
051
052 /** Rounding mode for divisions. **/
053 private RoundingMode roundingMode = RoundingMode.HALF_UP;
054
055 /*** BigDecimal scale ***/
056 private int scale = 64;
057
058 /** Build an instance from a BigDecimal.
059 * @param val value of the instance
060 */
061 public BigReal(BigDecimal val) {
062 d = val;
063 }
064
065 /** Build an instance from a BigInteger.
066 * @param val value of the instance
067 */
068 public BigReal(BigInteger val) {
069 d = new BigDecimal(val);
070 }
071
072 /** Build an instance from an unscaled BigInteger.
073 * @param unscaledVal unscaled value
074 * @param scale scale to use
075 */
076 public BigReal(BigInteger unscaledVal, int scale) {
077 d = new BigDecimal(unscaledVal, scale);
078 }
079
080 /** Build an instance from an unscaled BigInteger.
081 * @param unscaledVal unscaled value
082 * @param scale scale to use
083 * @param mc to used
084 */
085 public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
086 d = new BigDecimal(unscaledVal, scale, mc);
087 }
088
089 /** Build an instance from a BigInteger.
090 * @param val value of the instance
091 * @param mc context to use
092 */
093 public BigReal(BigInteger val, MathContext mc) {
094 d = new BigDecimal(val, mc);
095 }
096
097 /** Build an instance from a characters representation.
098 * @param in character representation of the value
099 */
100 public BigReal(char[] in) {
101 d = new BigDecimal(in);
102 }
103
104 /** Build an instance from a characters representation.
105 * @param in character representation of the value
106 * @param offset offset of the first character to analyze
107 * @param len length of the array slice to analyze
108 */
109 public BigReal(char[] in, int offset, int len) {
110 d = new BigDecimal(in, offset, len);
111 }
112
113 /** Build an instance from a characters representation.
114 * @param in character representation of the value
115 * @param offset offset of the first character to analyze
116 * @param len length of the array slice to analyze
117 * @param mc context to use
118 */
119 public BigReal(char[] in, int offset, int len, MathContext mc) {
120 d = new BigDecimal(in, offset, len, mc);
121 }
122
123 /** Build an instance from a characters representation.
124 * @param in character representation of the value
125 * @param mc context to use
126 */
127 public BigReal(char[] in, MathContext mc) {
128 d = new BigDecimal(in, mc);
129 }
130
131 /** Build an instance from a double.
132 * @param val value of the instance
133 */
134 public BigReal(double val) {
135 d = new BigDecimal(val);
136 }
137
138 /** Build an instance from a double.
139 * @param val value of the instance
140 * @param mc context to use
141 */
142 public BigReal(double val, MathContext mc) {
143 d = new BigDecimal(val, mc);
144 }
145
146 /** Build an instance from an int.
147 * @param val value of the instance
148 */
149 public BigReal(int val) {
150 d = new BigDecimal(val);
151 }
152
153 /** Build an instance from an int.
154 * @param val value of the instance
155 * @param mc context to use
156 */
157 public BigReal(int val, MathContext mc) {
158 d = new BigDecimal(val, mc);
159 }
160
161 /** Build an instance from a long.
162 * @param val value of the instance
163 */
164 public BigReal(long val) {
165 d = new BigDecimal(val);
166 }
167
168 /** Build an instance from a long.
169 * @param val value of the instance
170 * @param mc context to use
171 */
172 public BigReal(long val, MathContext mc) {
173 d = new BigDecimal(val, mc);
174 }
175
176 /** Build an instance from a String representation.
177 * @param val character representation of the value
178 */
179 public BigReal(String val) {
180 d = new BigDecimal(val);
181 }
182
183 /** Build an instance from a String representation.
184 * @param val character representation of the value
185 * @param mc context to use
186 */
187 public BigReal(String val, MathContext mc) {
188 d = new BigDecimal(val, mc);
189 }
190
191 /***
192 * Gets the rounding mode for division operations
193 * The default is {@code RoundingMode.HALF_UP}
194 * @return the rounding mode.
195 * @since 2.1
196 */
197 public RoundingMode getRoundingMode() {
198 return roundingMode;
199 }
200
201 /***
202 * Sets the rounding mode for decimal divisions.
203 * @param roundingMode rounding mode for decimal divisions
204 * @since 2.1
205 */
206 public void setRoundingMode(RoundingMode roundingMode) {
207 this.roundingMode = roundingMode;
208 }
209
210 /***
211 * Sets the scale for division operations.
212 * The default is 64
213 * @return the scale
214 * @since 2.1
215 */
216 public int getScale() {
217 return scale;
218 }
219
220 /***
221 * Sets the scale for division operations.
222 * @param scale scale for division operations
223 * @since 2.1
224 */
225 public void setScale(int scale) {
226 this.scale = scale;
227 }
228
229 /** {@inheritDoc} */
230 public BigReal add(BigReal a) {
231 return new BigReal(d.add(a.d));
232 }
233
234 /** {@inheritDoc} */
235 public BigReal subtract(BigReal a) {
236 return new BigReal(d.subtract(a.d));
237 }
238
239 /** {@inheritDoc} */
240 public BigReal divide(BigReal a) throws ArithmeticException {
241 return new BigReal(d.divide(a.d, scale, roundingMode));
242 }
243
244 /** {@inheritDoc} */
245 public BigReal multiply(BigReal a) {
246 return new BigReal(d.multiply(a.d));
247 }
248
249 /** {@inheritDoc} */
250 public int compareTo(BigReal a) {
251 return d.compareTo(a.d);
252 }
253
254 /** Get the double value corresponding to the instance.
255 * @return double value corresponding to the instance
256 */
257 public double doubleValue() {
258 return d.doubleValue();
259 }
260
261 /** Get the BigDecimal value corresponding to the instance.
262 * @return BigDecimal value corresponding to the instance
263 */
264 public BigDecimal bigDecimalValue() {
265 return d;
266 }
267
268 /** {@inheritDoc} */
269 @Override
270 public boolean equals(Object other) {
271 if (this == other){
272 return true;
273 }
274
275 if (other instanceof BigReal){
276 return d.equals(((BigReal) other).d);
277 }
278 return false;
279 }
280
281 /** {@inheritDoc} */
282 @Override
283 public int hashCode() {
284 return d.hashCode();
285 }
286
287 /** {@inheritDoc} */
288 public Field<BigReal> getField() {
289 return BigRealField.getInstance();
290 }
291
292 }