KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
3  * Copyright (C) 2005 - 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 javolution.lang.MathLib;
12 import javolution.lang.Text;
13 import javolution.lang.TypeFormat;
14 import javolution.xml.XmlElement;
15 import javolution.xml.XmlFormat;
16
17 /**
18  * <p> This class represents a 32 bits floating point number.</p>
19  *
20  * @author <a HREF="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
21  * @version 2.0, June 6, 2004
22  */

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

30     protected static final XmlFormat<Float32> XML = new XmlFormat<Float32>(
31             Float32.class) {
32         public void format(Float32 obj, XmlElement xml) {
33             xml.setAttribute("value", obj._value);
34         }
35
36         public Float32 parse(XmlElement xml) {
37             return Float32.valueOf(xml.getAttribute("value", 0.0f));
38         }
39     };
40
41     /**
42      * Holds the factory used to produce 32 bits float instances.
43      */

44     private static final Factory<Float32> FACTORY = new Factory<Float32>() {
45
46         public Float32 create() {
47             return new Float32();
48         }
49     };
50
51     /**
52      * The 32 bits floating point representing zero.
53      */

54     public static final Float32 ZERO = valueOf(0.0f).moveHeap();
55
56     /**
57      * The 32 bits floating point representing one.
58      */

59     public static final Float32 ONE = valueOf(1.0f).moveHeap();
60
61     /**
62      * The associated float value.
63      */

64     private float _value;
65
66     /**
67      * Default constructor.
68      */

69     private Float32() {
70     }
71
72     /**
73      * Returns the 32 bits float from the specified <code>float</code> value.
74      *
75      * @param floatValue the <code>float</code> value for this number.
76      * @return the corresponding number.
77      * @see #floatValue()
78      */

79     public static Float32 valueOf(float floatValue) {
80         Float32 r = FACTORY.object();
81         r._value = floatValue;
82         return r;
83     }
84
85     /**
86      * Returns the number for the specified character sequence.
87      *
88      * @param chars the character sequence.
89      * @return the corresponding number.
90      */

91     public static Float32 valueOf(CharSequence JavaDoc chars) {
92         Float32 r = FACTORY.object();
93         r._value = TypeFormat.parseFloat(chars);
94         return r;
95     }
96
97     /**
98      * Indicates if this number is infinite.
99      *
100      * @return <code>true</code> if this number is infinite;
101      * <code>false</code> otherwise.
102      */

103     public boolean isInfinite() {
104         return Float.isInfinite(_value);
105     }
106
107     /**
108      * Indicates if this number is not a number.
109      *
110      * @return <code>true</code> if this number is NaN;
111      * <code>false</code> otherwise.
112      */

113     public boolean isNaN() {
114         return Float.isNaN(_value);
115     }
116
117     /**
118      * Returns the opposite of this number.
119      *
120      * @return <code>-this</code>.
121      */

122     public Float32 opposite() {
123         Float32 r = FACTORY.object();
124         r._value = -this._value;
125         return r;
126     }
127
128     /**
129      * Returns the sum of this number with the one specified.
130      *
131      * @param that the number to be added.
132      * @return <code>this + that</code>.
133      */

134     public Float32 plus(Float32 that) {
135         Float32 r = FACTORY.object();
136         r._value = this._value + that._value;
137         return r;
138     }
139
140     /**
141      * Returns the difference between this number and the one specified.
142      *
143      * @param that the number to be subtracted.
144      * @return <code>this - that</code>.
145      */

146     public Float32 minus(Float32 that) {
147         Float32 r = FACTORY.object();
148         r._value = this._value - that._value;
149         return r;
150     }
151
152     /**
153      * Returns the product of this number with the one specified.
154      *
155      * @param that the number multiplier.
156      * @return <code>this * that</code>.
157      */

158     public Float32 times(Float32 that) {
159         Float32 r = FACTORY.object();
160         r._value = this._value * that._value;
161         return r;
162     }
163
164     /**
165      * Returns the reciprocal of this number.
166      *
167      * @return <code>1 / this</code>.
168      */

169     public Float32 reciprocal() {
170         Float32 r = FACTORY.object();
171         r._value = 1.0f / this._value;
172         return r;
173     }
174
175     /**
176      * Returns this number divided by the one specified.
177      *
178      * @param that the number divisor.
179      * @return <code>this / that</code>.
180      */

181     public Float32 divide(Float32 that) {
182         Float32 r = FACTORY.object();
183         r._value = this._value / that._value;
184         return r;
185     }
186
187     /**
188      * Compares the {@link #norm norm} of this number with that number.
189      *
190      * @return <code>|this| > |that|</code>
191      */

192     public boolean isLargerThan(Float32 that) {
193         return MathLib.abs(this._value) > MathLib.abs(that._value);
194     }
195
196     /**
197      * Returns the norm of this number.
198      *
199      * @return <code>abs(this)</code>.
200      */

201     public Float32 norm() {
202         Float32 r = FACTORY.object();
203         r._value = MathLib.abs(this._value);
204         return r;
205     }
206
207     /**
208      * Returns the positive square root of this number.
209      *
210      * @return <code>sqrt(this)</code>.
211      */

212     public Float32 sqrt() {
213         Float32 r = FACTORY.object();
214         r._value = (float) MathLib.sqrt(this._value);
215         return r;
216     }
217
218     /**
219      * Returns the exponential number <i>e</i> raised to the power of this
220      * number.
221      *
222      * @return <code>exp(this)</code>.
223      */

224     public Float32 exp() {
225         Float32 r = FACTORY.object();
226         r._value = (float) MathLib.exp(this._value);
227         return r;
228     }
229
230     /**
231      * Returns the natural logarithm (base e) of this number.
232      *
233      * @return <code>log(this)</code>.
234      */

235     public Float32 log() {
236         Float32 r = FACTORY.object();
237         r._value = (float) MathLib.log(this._value);
238         return r;
239     }
240
241     /**
242      * Returns this number raised to the specified power.
243      *
244      * @param e the exponent.
245      * @return <code>this**e</code>.
246      */

247     public Float32 pow(double e) {
248         Float32 r = FACTORY.object();
249         r._value = (float) MathLib.pow(this._value, e);
250         return r;
251     }
252
253     /**
254      * Returns this number raised to the power of the specified exponent.
255      *
256      * @param that the exponent.
257      * @return <code>this**that</code>.
258      */

259     public Float32 pow(Float32 that) {
260         Float32 r = FACTORY.object();
261         r._value = (float) MathLib.pow(this._value, that._value);
262         return r;
263     }
264
265     /**
266      * Returns the decimal text representation of this number.
267      *
268      * @return the text representation of this number.
269      */

270     public Text toText() {
271         return Text.valueOf(_value);
272     }
273
274     /**
275      * Compares this number against the specified object.
276      *
277      * @param that the object to compare with.
278      * @return <code>true</code> if the objects are the same;
279      * <code>false</code> otherwise.
280      */

281     public boolean equals(Object JavaDoc that) {
282         return (that instanceof Float32)
283                 && (this._value == ((Float32) that)._value);
284     }
285
286     /**
287      * Returns the hash code for this number.
288      *
289      * @return the hash code value.
290      */

291     public int hashCode() {
292         int h = Float.floatToIntBits(_value);
293         h += ~(h << 9);
294         h ^= (h >>> 14);
295         h += (h << 4);
296         return h ^ (h >>> 10);
297     }
298
299     // Implements abstract method.
300
public long longValue() {
301         return (long) _value;
302     }
303
304     // Implements abstract method.
305
public double doubleValue() {
306         return _value;
307     }
308
309     // Implements Comparable.
310
public int compareTo(Float32 that) {
311         if (this._value < that._value) {
312             return -1;
313         } else if (this._value > that._value) {
314             return 1;
315         } else {
316             int l1 = Float.floatToIntBits(this._value);
317             int l2 = Float.floatToIntBits(that._value);
318             return (l1 == l2 ? 0 : (l1 < l2 ? -1 : 1));
319         }
320     }
321
322     private static final long serialVersionUID = 1L;
323 }
Popular Tags