1/*
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
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17package org.apache.commons.math.util;
18
19
20import java.io.Serializable;
21import java.math.BigDecimal;
22import java.math.BigInteger;
23import java.math.MathContext;
24import java.math.RoundingMode;
25
26import org.apache.commons.math.Field;
27import org.apache.commons.math.FieldElement;
28
29/**
30 * Arbitrary precision decimal number.
31 * <p>
32 * This class is a simple wrapper around the standard <code>BigDecimal</code>
33 * in order to implement the {@link FieldElement} interface.
34 * </p>
35 * @since 2.0
36 * @version $Revision: 925812 $ $Date: 2010-03-21 16:49:31 +0100 (dim. 21 mars 2010) $
37 */
38public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {
39
40    /** A big real representing 0. */
41    public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);
42
43    /** A big real representing 1. */
44    public static final BigReal ONE = new BigReal(BigDecimal.ONE);
45
46    /** Serializable version identifier. */
47    private static final long serialVersionUID = 4984534880991310382L;
48
49    /** Underlying BigDecimal. */
50    private final BigDecimal d;
51
52    /** Rounding mode for divisions. **/
53    private RoundingMode roundingMode = RoundingMode.HALF_UP;
54
55    /*** BigDecimal scale ***/
56    private int scale = 64;
57
58    /** Build an instance from a BigDecimal.
59     * @param val value of the instance
60     */
61    public BigReal(BigDecimal val) {
62        d =  val;
63    }
64
65    /** Build an instance from a BigInteger.
66     * @param val value of the instance
67     */
68    public BigReal(BigInteger val) {
69        d = new BigDecimal(val);
70    }
71
72    /** Build an instance from an unscaled BigInteger.
73     * @param unscaledVal unscaled value
74     * @param scale scale to use
75     */
76    public BigReal(BigInteger unscaledVal, int scale) {
77        d = new BigDecimal(unscaledVal, scale);
78    }
79
80    /** Build an instance from an unscaled BigInteger.
81     * @param unscaledVal unscaled value
82     * @param scale scale to use
83     * @param mc to used
84     */
85    public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
86        d = new BigDecimal(unscaledVal, scale, mc);
87    }
88
89    /** Build an instance from a BigInteger.
90     * @param val value of the instance
91     * @param mc context to use
92     */
93    public BigReal(BigInteger val, MathContext mc) {
94        d = new BigDecimal(val, mc);
95    }
96
97    /** Build an instance from a characters representation.
98     * @param in character representation of the value
99     */
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}
293