~ubuntu-branches/ubuntu/maverick/commons-math/maverick

« back to all changes in this revision

Viewing changes to src/main/java/org/apache/commons/math/util/BigReal.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-08-22 01:13:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090822011325-hi4peq1ua5weguwn
Tags: 2.0-1
* New upstream release.
* Set Maintainer field to Debian Java Team
* Add myself as Uploaders
* Switch to Quilt patch system:
  - Refresh all patchs
  - Remove B-D on dpatch, Add B-D on quilt
  - Include patchsys-quilt.mk in debian/rules
* Bump Standards-Version to 3.8.3:
  - Add a README.source to describe patch system
* Maven POMs:
  - Add a Build-Depends-Indep dependency on maven-repo-helper
  - Use mh_installpom and mh_installjar to install the POM and the jar to the
    Maven repository
* Use default-jdk/jre:
  - Depends on java5-runtime-headless
  - Build-Depends on default-jdk
  - Use /usr/lib/jvm/default-java as JAVA_HOME
* Move api documentation to /usr/share/doc/libcommons-math-java/api
* Build-Depends on junit4 instead of junit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
17
package org.apache.commons.math.util;
 
18
 
 
19
 
 
20
import java.io.Serializable;
 
21
import java.math.BigDecimal;
 
22
import java.math.BigInteger;
 
23
import java.math.MathContext;
 
24
 
 
25
import org.apache.commons.math.Field;
 
26
import org.apache.commons.math.FieldElement;
 
27
 
 
28
/**
 
29
 * Arbitrary precision decimal number.
 
30
 * <p>
 
31
 * This class is a simple wrapper around the standard <code>BigDecimal</code>
 
32
 * in order to implement the {@link FieldElement} interface.
 
33
 * </p>
 
34
 * @since 2.0
 
35
 * @version $Revision: 795963 $ $Date: 2009-07-20 15:23:43 -0400 (Mon, 20 Jul 2009) $
 
36
 */
 
37
public class BigReal implements FieldElement<BigReal>, Comparable<BigReal>, Serializable {
 
38
 
 
39
    /** Serializable version identifier. */
 
40
    private static final long serialVersionUID = 7887631840434052850L;
 
41
 
 
42
    /** A big real representing 0. */
 
43
    public static final BigReal ZERO = new BigReal(BigDecimal.ZERO);
 
44
 
 
45
    /** A big real representing 1. */
 
46
    public static final BigReal ONE = new BigReal(BigDecimal.ONE);
 
47
 
 
48
    /** Underlying BigDecimal. */
 
49
    private final BigDecimal d;
 
50
 
 
51
    /** Build an instance from a BigDecimal.
 
52
     * @param val value of the instance
 
53
     */
 
54
    public BigReal(BigDecimal val) {
 
55
        d =  val;
 
56
    }
 
57
 
 
58
    /** Build an instance from a BigInteger.
 
59
     * @param val value of the instance
 
60
     */
 
61
    public BigReal(BigInteger val) {
 
62
        d = new BigDecimal(val);
 
63
    }
 
64
 
 
65
    /** Build an instance from an unscaled BigInteger.
 
66
     * @param unscaledVal unscaled value
 
67
     * @param scale scale to use
 
68
     */
 
69
    public BigReal(BigInteger unscaledVal, int scale) {
 
70
        d = new BigDecimal(unscaledVal, scale);
 
71
    }
 
72
 
 
73
    /** Build an instance from an unscaled BigInteger.
 
74
     * @param unscaledVal unscaled value
 
75
     * @param scale scale to use
 
76
     * @param mc to used
 
77
     */
 
78
    public BigReal(BigInteger unscaledVal, int scale, MathContext mc) {
 
79
        d = new BigDecimal(unscaledVal, scale, mc);
 
80
    }
 
81
 
 
82
    /** Build an instance from a BigInteger.
 
83
     * @param val value of the instance
 
84
     * @param mc context to use
 
85
     */
 
86
    public BigReal(BigInteger val, MathContext mc) {
 
87
        d = new BigDecimal(val, mc);
 
88
    }
 
89
 
 
90
    /** Build an instance from a characters representation.
 
91
     * @param in character representation of the value
 
92
     */
 
93
    public BigReal(char[] in) {
 
94
        d = new BigDecimal(in);
 
95
    }
 
96
 
 
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
 
101
     */
 
102
    public BigReal(char[] in, int offset, int len) {
 
103
        d = new BigDecimal(in, offset, len);
 
104
    }
 
105
 
 
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
 
111
     */
 
112
    public BigReal(char[] in, int offset, int len, MathContext mc) {
 
113
        d = new BigDecimal(in, offset, len, mc);
 
114
    }
 
115
 
 
116
    /** Build an instance from a characters representation.
 
117
     * @param in character representation of the value
 
118
     * @param mc context to use
 
119
     */
 
120
    public BigReal(char[] in, MathContext mc) {
 
121
        d = new BigDecimal(in, mc);
 
122
    }
 
123
 
 
124
    /** Build an instance from a double.
 
125
     * @param val value of the instance
 
126
     */
 
127
    public BigReal(double val) {
 
128
        d = new BigDecimal(val);
 
129
    }
 
130
 
 
131
    /** Build an instance from a double.
 
132
     * @param val value of the instance
 
133
     * @param mc context to use
 
134
     */
 
135
    public BigReal(double val, MathContext mc) {
 
136
        d = new BigDecimal(val, mc);
 
137
    }
 
138
 
 
139
    /** Build an instance from an int.
 
140
     * @param val value of the instance
 
141
     */
 
142
    public BigReal(int val) {
 
143
        d = new BigDecimal(val);
 
144
    }
 
145
 
 
146
    /** Build an instance from an int.
 
147
     * @param val value of the instance
 
148
     * @param mc context to use
 
149
     */
 
150
    public BigReal(int val, MathContext mc) {
 
151
        d = new BigDecimal(val, mc);
 
152
    }
 
153
 
 
154
    /** Build an instance from a long.
 
155
     * @param val value of the instance
 
156
     */
 
157
    public BigReal(long val) {
 
158
        d = new BigDecimal(val);
 
159
    }
 
160
 
 
161
    /** Build an instance from a long.
 
162
     * @param val value of the instance
 
163
     * @param mc context to use
 
164
     */
 
165
    public BigReal(long val, MathContext mc) {
 
166
        d = new BigDecimal(val, mc);
 
167
    }
 
168
 
 
169
    /** Build an instance from a String representation.
 
170
     * @param val character representation of the value
 
171
     */
 
172
    public BigReal(String val) {
 
173
        d = new BigDecimal(val);
 
174
    }
 
175
 
 
176
    /** Build an instance from a String representation.
 
177
     * @param val character representation of the value
 
178
     * @param mc context to use
 
179
     */
 
180
    public BigReal(String val, MathContext mc)  {
 
181
        d = new BigDecimal(val, mc);
 
182
    }
 
183
 
 
184
    /** {@inheritDoc} */
 
185
    public BigReal add(BigReal a) {
 
186
        return new BigReal(d.add(a.d));
 
187
    }
 
188
 
 
189
    /** {@inheritDoc} */
 
190
    public BigReal subtract(BigReal a) {
 
191
        return new BigReal(d.subtract(a.d));
 
192
    }
 
193
 
 
194
    /** {@inheritDoc} */
 
195
    public BigReal divide(BigReal a) throws ArithmeticException {
 
196
        return new BigReal(d.divide(a.d));
 
197
    }
 
198
 
 
199
    /** {@inheritDoc} */
 
200
    public BigReal multiply(BigReal a) {
 
201
        return new BigReal(d.multiply(a.d));
 
202
    }
 
203
 
 
204
    /** {@inheritDoc} */
 
205
    public int compareTo(BigReal a) {
 
206
        return d.compareTo(a.d);
 
207
    }
 
208
 
 
209
    /** Get the double value corresponding to the instance.
 
210
     * @return double value corresponding to the instance
 
211
     */
 
212
    public double doubleValue() {
 
213
        return d.doubleValue();
 
214
    }
 
215
 
 
216
    /** Get the BigDecimal value corresponding to the instance.
 
217
     * @return BigDecimal value corresponding to the instance
 
218
     */
 
219
    public BigDecimal bigDecimalValue() {
 
220
        return d;
 
221
    }
 
222
 
 
223
    /** {@inheritDoc} */
 
224
    @Override
 
225
    public boolean equals(Object other) {
 
226
        try {
 
227
            if (other == null) {
 
228
                return false;
 
229
            }
 
230
            return d.equals(((BigReal) other).d);
 
231
        } catch (ClassCastException cce) {
 
232
            return false;
 
233
        }
 
234
    }
 
235
 
 
236
    /** {@inheritDoc} */
 
237
    @Override
 
238
    public int hashCode() {
 
239
        return d.hashCode();
 
240
    }
 
241
 
 
242
    /** {@inheritDoc} */
 
243
    public Field<BigReal> getField() {
 
244
        return BigRealField.getInstance();
 
245
    }
 
246
 
 
247
}