KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > MathDouble


1 package JSci.maths;
2
3 import java.lang.Comparable JavaDoc;
4 import java.lang.Double JavaDoc;
5
6 import JSci.GlobalSettings;
7 import JSci.maths.groups.AbelianGroup;
8 import JSci.maths.fields.*;
9
10 /**
11 * The MathDouble class encapsulates double numbers.
12 * Methods will automatically promote objects from subsets.
13 * @see JSci.maths.fields.RealField
14 * @version 1.1
15 * @author Mark Hale
16 */

17 public final class MathDouble extends Number JavaDoc implements Comparable JavaDoc, Field.Member {
18         private static final long serialVersionUID = 8616680319093653108L;
19
20         private final double x;
21         /**
22         * Constructs a double number.
23         */

24         public MathDouble(final double num) {
25                 x=num;
26         }
27         /**
28         * Constructs the double number represented by a string.
29         * @param s a string representing a double number.
30         * @exception NumberFormatException if the string does not contain a parsable number.
31         */

32         public MathDouble(final String JavaDoc s) throws NumberFormatException JavaDoc {
33                 x=Double.parseDouble(s);
34         }
35         /**
36         * Compares two numbers for equality.
37         * @param obj a number.
38         */

39         public boolean equals(Object JavaDoc obj) {
40         return equals(obj, GlobalSettings.ZERO_TOL);
41         }
42     public boolean equals(Object JavaDoc obj, double tol) {
43                 if(obj instanceof Number JavaDoc) {
44                         return Math.abs(x-((Number JavaDoc)obj).doubleValue()) <= tol;
45                 } else
46                         return false;
47     }
48     public int hashCode() {
49         return (int) x;
50     }
51         /**
52         * Compares two numbers.
53         * @param obj a number.
54         * @return a negative value if <code>this&lt;obj</code>,
55         * zero if <code>this==obj</code>,
56         * and a positive value if <code>this&gt;obj</code>.
57         */

58         public int compareTo(Object JavaDoc obj) throws IllegalArgumentException JavaDoc {
59                 if(obj!=null && (obj instanceof Number JavaDoc)) {
60             double objValue = ((Number JavaDoc)obj).doubleValue();
61                         if(Math.abs(x-objValue) <= GlobalSettings.ZERO_TOL)
62                                 return 0;
63                         else
64                                 return (int)(x-objValue);
65                 } else
66                         throw new IllegalArgumentException JavaDoc("Invalid object: "+obj.getClass());
67         }
68         /**
69         * Returns a string representing the value of this double number.
70         */

71         public String JavaDoc toString() {
72                 return Double.toString(x);
73         }
74         /**
75         * Returns the double value.
76         */

77         public double value() {
78                 return x;
79         }
80         public int intValue() {
81                 return (int) x;
82         }
83         public long longValue() {
84                 return (long) x;
85         }
86         public float floatValue() {
87                 return (float) x;
88         }
89         public double doubleValue() {
90                 return x;
91         }
92         /**
93         * Returns true if the number is within the zero tolerance.
94         */

95         public static boolean isZero(double x) {
96                 return (Math.abs(x) <= GlobalSettings.ZERO_TOL);
97         }
98         /**
99         * Returns true if this number is NaN.
100         */

101         public boolean isNaN() {
102                 return (x==Double.NaN);
103         }
104         /**
105         * Returns true if this number is infinite.
106         */

107         public boolean isInfinite() {
108                 return (x==Double.POSITIVE_INFINITY) || (x==Double.NEGATIVE_INFINITY);
109         }
110     public Object JavaDoc getSet() {
111         return RealField.getInstance();
112     }
113         /**
114         * Returns the negative of this number.
115         */

116         public AbelianGroup.Member negate() {
117                 return new MathDouble(-x);
118         }
119         /**
120         * Returns the inverse of this number.
121         */

122         public Field.Member inverse() {
123                 return new MathDouble(1.0/x);
124         }
125         /**
126         * Returns the addition of this number and another.
127         */

128         public AbelianGroup.Member add(final AbelianGroup.Member n) {
129                 if(n instanceof Number JavaDoc)
130                         return add(((Number JavaDoc)n).doubleValue());
131                 else
132                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
133         }
134         /**
135         * Returns the addition of this double number and another.
136         */

137         public MathDouble add(final MathDouble n) {
138                 return add(n.x);
139         }
140     public MathDouble add(double y) {
141         return new MathDouble(x+y);
142     }
143         /**
144         * Returns the subtraction of this number and another.
145         */

146         public AbelianGroup.Member subtract(final AbelianGroup.Member n) {
147                 if(n instanceof Number JavaDoc)
148                         return subtract(((Number JavaDoc)n).doubleValue());
149                 else
150                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
151         }
152         /**
153         * Returns the subtraction of this double number and another.
154         */

155         public MathDouble subtract(final MathDouble n) {
156                 return subtract(n.x);
157         }
158     public MathDouble subtract(double y) {
159         return new MathDouble(x-y);
160     }
161         /**
162         * Returns the multiplication of this number and another.
163         */

164         public Ring.Member multiply(final Ring.Member n) {
165                 if(n instanceof Number JavaDoc)
166                         return multiply(((Number JavaDoc)n).doubleValue());
167                 else
168                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
169         }
170         /**
171         * Returns the multiplication of this double number and another.
172         */

173         public MathDouble multiply(final MathDouble n) {
174                 return multiply(n.x);
175         }
176         public MathDouble multiply(double y) {
177                 return new MathDouble(x*y);
178         }
179         /**
180         * Returns the division of this number and another.
181         */

182         public Field.Member divide(final Field.Member n) {
183                 if(n instanceof Number JavaDoc)
184                         return divide(((Number JavaDoc)n).doubleValue());
185                 else
186                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
187         }
188         /**
189         * Returns the division of this double number and another.
190         */

191         public MathDouble divide(final MathDouble n) {
192                 return divide(n.x);
193         }
194         public MathDouble divide(double y) {
195                 return new MathDouble(x/y);
196         }
197
198 //===========
199
// FUNCTIONS
200
//===========
201

202 // EXP
203

204         /**
205         * Returns the exponential number e(2.718...) raised to the power of a number.
206         */

207         public static MathDouble exp(final MathDouble x) {
208                 return exp(x.x);
209         }
210     public static MathDouble exp(Number JavaDoc x) {
211         return exp(x.doubleValue());
212     }
213     public static MathDouble exp(double x) {
214         return new MathDouble(Math.exp(x));
215     }
216
217 // LOG
218

219         /**
220         * Returns the natural logarithm (base e) of a number.
221         */

222         public static MathDouble log(final MathDouble x) {
223                 return log(x.x);
224         }
225     public static MathDouble log(Number JavaDoc x) {
226         return log(x.doubleValue());
227     }
228     public static MathDouble log(double x) {
229         return new MathDouble(Math.log(x));
230     }
231
232 // SIN
233

234         /**
235         * Returns the trigonometric sine of an angle.
236         * @param x an angle that is measured in radians
237         */

238         public static MathDouble sin(final MathDouble x) {
239                 return sin(x.x);
240         }
241     public static MathDouble sin(Number JavaDoc x) {
242         return sin(x.doubleValue());
243     }
244     public static MathDouble sin(double x) {
245         return new MathDouble(Math.sin(x));
246     }
247
248 // COS
249

250         /**
251         * Returns the trigonometric cosine of an angle.
252         * @param x an angle that is measured in radians
253         */

254         public static MathDouble cos(final MathDouble x) {
255                 return cos(x.x);
256         }
257     public static MathDouble cos(Number JavaDoc x) {
258         return cos(x.doubleValue());
259     }
260     public static MathDouble cos(double x) {
261         return new MathDouble(Math.cos(x));
262     }
263
264 // TAN
265

266         /**
267         * Returns the trigonometric tangent of an angle.
268         * @param x an angle that is measured in radians
269         */

270         public static MathDouble tan(final MathDouble x) {
271                 return tan(x.x);
272         }
273     public static MathDouble tan(Number JavaDoc x) {
274         return tan(x.doubleValue());
275     }
276     public static MathDouble tan(double x) {
277         return new MathDouble(Math.tan(x));
278     }
279
280 // SINH
281

282         /**
283         * Returns the hyperbolic sine of a number.
284         */

285         public static MathDouble sinh(final MathDouble x) {
286                 return sinh(x.x);
287         }
288     public static MathDouble sinh(Number JavaDoc x) {
289         return sinh(x.doubleValue());
290     }
291     public static MathDouble sinh(double x) {
292         return new MathDouble(ExtraMath.sinh(x));
293     }
294
295 // COSH
296

297         /**
298         * Returns the hyperbolic cosine of a number.
299         */

300         public static MathDouble cosh(final MathDouble x) {
301                 return cosh(x.x);
302         }
303     public static MathDouble cosh(Number JavaDoc x) {
304         return cosh(x.doubleValue());
305     }
306     public static MathDouble cosh(double x) {
307         return new MathDouble(ExtraMath.cosh(x));
308     }
309
310 // TANH
311

312         /**
313         * Returns the hyperbolic tangent of a number.
314         */

315         public static MathDouble tanh(final MathDouble x) {
316                 return tanh(x.x);
317         }
318     public static MathDouble tanh(Number JavaDoc x) {
319         return tanh(x.doubleValue());
320     }
321     public static MathDouble tanh(double x) {
322         return new MathDouble(ExtraMath.tanh(x));
323     }
324
325 // INVERSE SIN
326

327         /**
328         * Returns the arc sine of a number.
329         */

330         public static MathDouble asin(final MathDouble x) {
331                 return asin(x.x);
332         }
333     public static MathDouble asin(Number JavaDoc x) {
334         return asin(x.doubleValue());
335     }
336     public static MathDouble asin(double x) {
337         return new MathDouble(Math.asin(x));
338     }
339
340 // INVERSE COS
341

342         /**
343         * Returns the arc cosine of a number.
344         */

345         public static MathDouble acos(final MathDouble x) {
346                 return acos(x.x);
347         }
348     public static MathDouble acos(Number JavaDoc x) {
349         return acos(x.doubleValue());
350     }
351     public static MathDouble acos(double x) {
352                 return new MathDouble(Math.acos(x));
353         }
354
355 // INVERSE TAN
356

357         /**
358         * Returns the arc tangent of a number.
359         */

360         public static MathDouble atan(final MathDouble x) {
361                 return atan(x.x);
362         }
363     public static MathDouble atan(Number JavaDoc x) {
364         return atan(x.doubleValue());
365     }
366     public static MathDouble atan(double x) {
367                 return new MathDouble(Math.atan(x));
368         }
369
370 // INVERSE SINH
371

372         /**
373         * Returns the arc hyperbolic sine of a number.
374         */

375         public static MathDouble asinh(final MathDouble x) {
376                 return asinh(x.x);
377         }
378     public static MathDouble asinh(Number JavaDoc x) {
379         return asinh(x.doubleValue());
380     }
381     public static MathDouble asinh(double x) {
382         return new MathDouble(ExtraMath.asinh(x));
383     }
384
385 // INVERSE COSH
386

387         /**
388         * Returns the arc hyperbolic cosine of a number.
389         */

390         public static MathDouble acosh(final MathDouble x) {
391                 return acosh(x.x);
392         }
393     public static MathDouble acosh(Number JavaDoc x) {
394         return acosh(x.doubleValue());
395     }
396     public static MathDouble acosh(double x) {
397         return new MathDouble(ExtraMath.acosh(x));
398     }
399
400 // INVERSE TANH
401

402         /**
403         * Returns the arc hyperbolic tangent of a number.
404         */

405         public static MathDouble atanh(final MathDouble x) {
406                 return atanh(x.x);
407         }
408     public static MathDouble atanh(Number JavaDoc x) {
409         return atanh(x.doubleValue());
410     }
411     public static MathDouble atanh(double x) {
412         return new MathDouble(ExtraMath.atanh(x));
413     }
414 }
415
Popular Tags