KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jscience > mathematics > numbers > Float64


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2006 - JScience (http://jscience.org/)
4  * All rights reserved.
5  *
6  * Permission to use, copy, modify, and distribute this software is
7  * freely granted, provided that this notice is preserved.
8  */

9 package org.jscience.mathematics.numbers;
10
11 import org.jscience.mathematics.structures.Field;
12
13 import javolution.lang.MathLib;
14 import javolution.text.Text;
15 import javolution.text.TypeFormat;
16 import javolution.xml.XMLFormat;
17 import javolution.xml.stream.XMLStreamException;
18
19 /**
20  * <p> This class represents a 64 bits floating point number.</p>
21  *
22  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
23  * @version 3.0, February 13, 2006
24  */

25 public final class Float64 extends Number JavaDoc<Float64> implements Field<Float64> {
26
27     /**
28      * Holds the default XML representation for 64 bits floating point numbers.
29      * This representation consists of a simple <code>value</code> attribute
30      * holding the {@link #toText() textual} representation.
31      */

32     protected static final XMLFormat<Float64> XML = new XMLFormat<Float64>(Float64.class) {
33
34         @Override JavaDoc
35         public Float64 newInstance(Class JavaDoc<Float64> cls, InputElement xml)
36                 throws XMLStreamException {
37             return Float64.valueOf(xml.getAttribute("value", 0.0));
38         }
39
40         public void write(Float64 float64, OutputElement xml)
41                 throws XMLStreamException {
42             xml.setAttribute("value", float64._value);
43         }
44
45         public void read(InputElement xml, Float64 float64) {
46             // Nothing to do, immutable.
47
}
48     };
49
50     /**
51      * Holds the factory used to produce 64 bits float instances.
52      */

53     private static final Factory<Float64> FACTORY = new Factory<Float64>() {
54
55         public Float64 create() {
56             return new Float64();
57         }
58     };
59
60     /**
61      * The 64 bits floating point representing zero.
62      */

63     public static final Float64 ZERO = new Float64(0.0);
64
65     /**
66      * The 64 bits floating point representing one.
67      */

68     public static final Float64 ONE = new Float64(1.0);
69
70     /**
71      * The associated double value.
72      */

73     private double _value;
74
75     /**
76      * Default constructor.
77      */

78     private Float64() {
79     }
80
81     /**
82      * Creates a 64 bits float having the specified <code>double</code> value.
83      *
84      * @param doubleValue the <code>double</code> value for this number.
85      */

86     private Float64(double doubleValue) {
87         _value = doubleValue;
88     }
89
90     /**
91      * Returns the 64 bits float from the specified <code>double</code> value.
92      *
93      * @param doubleValue the <code>double</code> value for this number.
94      * @return the corresponding number.
95      * @see #doubleValue()
96      */

97     public static Float64 valueOf(double doubleValue) {
98         Float64 r = FACTORY.object();
99         r._value = doubleValue;
100         return r;
101     }
102
103     /**
104      * Returns the number for the specified character sequence.
105      *
106      * @param chars the character sequence.
107      * @return the corresponding number.
108      */

109     public static Float64 valueOf(CharSequence JavaDoc chars) {
110         Float64 r = FACTORY.object();
111         r._value = TypeFormat.parseDouble(chars);
112         return r;
113     }
114
115     /**
116      * Indicates if this number is infinite.
117      *
118      * @return <code>true</code> if this number is infinite;
119      * <code>false</code> otherwise.
120      */

121     public boolean isInfinite() {
122         return Double.isInfinite(_value);
123     }
124
125     /**
126      * Indicates if this number is not a number.
127      *
128      * @return <code>true</code> if this number is NaN;
129      * <code>false</code> otherwise.
130      */

131     public boolean isNaN() {
132         return Double.isNaN(_value);
133     }
134
135     /**
136      * Returns the opposite of this number.
137      *
138      * @return <code>-this</code>.
139      */

140     public Float64 opposite() {
141         Float64 r = FACTORY.object();
142         r._value = -this._value;
143         return r;
144     }
145
146     /**
147      * Returns the sum of this number with the one specified.
148      *
149      * @param that the number to be added.
150      * @return <code>this + that</code>.
151      */

152     public Float64 plus(Float64 that) {
153         Float64 r = FACTORY.object();
154         r._value = this._value + that._value;
155         return r;
156     }
157
158     /**
159      * Returns the difference between this number and the one specified.
160      *
161      * @param that the number to be subtracted.
162      * @return <code>this - that</code>.
163      */

164     public Float64 minus(Float64 that) {
165         Float64 r = FACTORY.object();
166         r._value = this._value - that._value;
167         return r;
168     }
169
170     /**
171      * Returns the product of this number with the one specified.
172      *
173      * @param that the number multiplier.
174      * @return <code>this ยท that</code>.
175      */

176     public Float64 times(Float64 that) {
177         Float64 r = FACTORY.object();
178         r._value = this._value * that._value;
179         return r;
180     }
181
182     /**
183      * Returns the reciprocal of this number.
184      *
185      * @return <code>1 / this</code>.
186      */

187     public Float64 inverse() {
188         Float64 r = FACTORY.object();
189         r._value = 1.0 / this._value;
190         return r;
191     }
192
193     /**
194      * Returns this number divided by the one specified.
195      *
196      * @param that the number divisor.
197      * @return <code>this / that</code>.
198      */

199     public Float64 divide(Float64 that) {
200         Float64 r = FACTORY.object();
201         r._value = this._value / that._value;
202         return r;
203     }
204
205     /**
206      * Compares the absolute value of this number with that number.
207      *
208      * @return <code>|this| > |that|</code>
209      */

210     public boolean isLargerThan(Float64 that) {
211         return MathLib.abs(this._value) > MathLib.abs(that._value);
212     }
213
214     /**
215      * Returns the absolute value of this number.
216      *
217      * @return <code>|this|</code>.
218      */

219     public Float64 abs() {
220         Float64 r = FACTORY.object();
221         r._value = MathLib.abs(this._value);
222         return r;
223     }
224
225     /**
226      * Returns the positive square root of this number.
227      *
228      * @return <code>sqrt(this)</code>.
229      */

230     public Float64 sqrt() {
231         Float64 r = FACTORY.object();
232         r._value = MathLib.sqrt(this._value);
233         return r;
234     }
235
236     /**
237      * Returns the exponential number <i>e</i> raised to the power of this
238      * number.
239      *
240      * @return <code>exp(this)</code>.
241      */

242     public Float64 exp() {
243         Float64 r = FACTORY.object();
244         r._value = MathLib.exp(this._value);
245         return r;
246     }
247
248     /**
249      * Returns the natural logarithm (base e) of this number.
250      *
251      * @return <code>log(this)</code>.
252      */

253     public Float64 log() {
254         Float64 r = FACTORY.object();
255         r._value = MathLib.log(this._value);
256         return r;
257     }
258
259     /**
260      * Returns this number raised to the specified power.
261      *
262      * @param e the exponent.
263      * @return <code>this**e</code>.
264      */

265     public Float64 pow(double e) {
266         Float64 r = FACTORY.object();
267         r._value = MathLib.pow(this._value, e);
268         return r;
269     }
270
271     /**
272      * Returns this number raised to the power of the specified exponent.
273      *
274      * @param that the exponent.
275      * @return <code>this**that</code>.
276      */

277     public Float64 pow(Float64 that) {
278         Float64 r = FACTORY.object();
279         r._value = MathLib.pow(this._value, that._value);
280         return r;
281     }
282
283     /**
284      * Returns the decimal text representation of this number.
285      *
286      * @return the text representation of this number.
287      */

288     public Text toText() {
289         return Text.valueOf(_value);
290     }
291
292     /**
293      * Compares this number against the specified object.
294      *
295      * @param that the object to compare with.
296      * @return <code>true</code> if the objects are the same;
297      * <code>false</code> otherwise.
298      */

299     public boolean equals(Object JavaDoc that) {
300         return (that instanceof Float64)
301                 && (this._value == ((Float64) that)._value);
302     }
303
304     /**
305      * Returns the hash code for this number.
306      *
307      * @return the hash code value.
308      */

309     public int hashCode() {
310         int h = Float.floatToIntBits((float) _value);
311         h += ~(h << 9);
312         h ^= (h >>> 14);
313         h += (h << 4);
314         return h ^ (h >>> 10);
315     }
316
317     @Override JavaDoc
318     public long longValue() {
319         return (long) _value;
320     }
321
322     @Override JavaDoc
323     public double doubleValue() {
324         return _value;
325     }
326
327     @Override JavaDoc
328     public int compareTo(Float64 that) {
329         if (this._value < that._value) {
330             return -1;
331         } else if (this._value > that._value) {
332             return 1;
333         } else {
334             long l1 = Double.doubleToLongBits(this._value);
335             long l2 = Double.doubleToLongBits(that._value);
336             return (l1 == l2 ? 0 : (l1 < l2 ? -1 : 1));
337         }
338     }
339
340     private static final long serialVersionUID = 1L;
341 }
Popular Tags