001: /*
002: * JScience - Java(TM) Tools and Libraries for the Advancement of Sciences.
003: * Copyright (C) 2006 - JScience (http://jscience.org/)
004: * All rights reserved.
005: *
006: * Permission to use, copy, modify, and distribute this software is
007: * freely granted, provided that this notice is preserved.
008: */
009: package org.jscience.mathematics.number;
010:
011: import org.jscience.mathematics.structure.Field;
012:
013: import javolution.context.ObjectFactory;
014: import javolution.lang.MathLib;
015: import javolution.text.Text;
016: import javolution.text.TypeFormat;
017: import javolution.xml.XMLFormat;
018: import javolution.xml.stream.XMLStreamException;
019:
020: /**
021: * <p> This class represents a 64 bits floating point number.</p>
022: *
023: * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
024: * @version 3.0, February 13, 2006
025: */
026: public final class Float64 extends Number<Float64> implements
027: Field<Float64> {
028:
029: /**
030: * Holds the default XML representation for 64 bits floating point numbers.
031: * This representation consists of a simple <code>value</code> attribute
032: * holding the {@link #toText() textual} representation.
033: */
034: static final XMLFormat<Float64> XML = new XMLFormat<Float64>(
035: Float64.class) {
036:
037: @Override
038: public Float64 newInstance(Class<Float64> cls, InputElement xml)
039: throws XMLStreamException {
040: return Float64.valueOf(xml.getAttribute("value", 0.0));
041: }
042:
043: public void write(Float64 float64, OutputElement xml)
044: throws XMLStreamException {
045: xml.setAttribute("value", float64._value);
046: }
047:
048: public void read(InputElement xml, Float64 float64) {
049: // Nothing to do, immutable.
050: }
051: };
052:
053: /**
054: * Holds the factory used to produce 64 bits float instances.
055: */
056: private static final ObjectFactory<Float64> FACTORY = new ObjectFactory<Float64>() {
057:
058: protected Float64 create() {
059: return new Float64();
060: }
061: };
062:
063: /**
064: * The 64 bits floating point representing zero.
065: */
066: public static final Float64 ZERO = new Float64(0.0);
067:
068: /**
069: * The 64 bits floating point representing one.
070: */
071: public static final Float64 ONE = new Float64(1.0);
072:
073: /**
074: * The associated double value.
075: */
076: private double _value;
077:
078: /**
079: * Default constructor.
080: */
081: private Float64() {
082: }
083:
084: /**
085: * Creates a 64 bits float having the specified <code>double</code> value.
086: *
087: * @param doubleValue the <code>double</code> value for this number.
088: */
089: private Float64(double doubleValue) {
090: _value = doubleValue;
091: }
092:
093: /**
094: * Returns the 64 bits float from the specified <code>double</code> value.
095: *
096: * @param doubleValue the <code>double</code> value for this number.
097: * @return the corresponding number.
098: * @see #doubleValue()
099: */
100: public static Float64 valueOf(double doubleValue) {
101: Float64 r = FACTORY.object();
102: r._value = doubleValue;
103: return r;
104: }
105:
106: /**
107: * Returns the number for the specified character sequence.
108: *
109: * @param chars the character sequence.
110: * @return the corresponding number.
111: */
112: public static Float64 valueOf(CharSequence chars) {
113: Float64 r = FACTORY.object();
114: r._value = TypeFormat.parseDouble(chars);
115: return r;
116: }
117:
118: /**
119: * Indicates if this number is infinite.
120: *
121: * @return <code>true</code> if this number is infinite;
122: * <code>false</code> otherwise.
123: */
124: public boolean isInfinite() {
125: return Double.isInfinite(_value);
126: }
127:
128: /**
129: * Indicates if this number is not a number.
130: *
131: * @return <code>true</code> if this number is NaN;
132: * <code>false</code> otherwise.
133: */
134: public boolean isNaN() {
135: return Double.isNaN(_value);
136: }
137:
138: /**
139: * Returns the closest integer value to this 64 bits floating point number.
140: *
141: * @return this number rounded to the nearest integer.
142: */
143: public long round() {
144: return MathLib.round(_value);
145: }
146:
147: /**
148: * Returns the opposite of this number.
149: *
150: * @return <code>-this</code>.
151: */
152: public Float64 opposite() {
153: Float64 r = FACTORY.object();
154: r._value = -this ._value;
155: return r;
156: }
157:
158: /**
159: * Returns the sum of this number with the one specified.
160: *
161: * @param that the number to be added.
162: * @return <code>this + that</code>.
163: */
164: public Float64 plus(Float64 that) {
165: Float64 r = FACTORY.object();
166: r._value = this ._value + that._value;
167: return r;
168: }
169:
170: /**
171: * Returns the sum of this number with the specified value.
172: *
173: * @param value the value to be added.
174: * @return <code>this + value</code>.
175: */
176: public Float64 plus(double value) {
177: Float64 r = FACTORY.object();
178: r._value = this ._value + value;
179: return r;
180: }
181:
182: /**
183: * Returns the difference between this number and the one specified.
184: *
185: * @param that the number to be subtracted.
186: * @return <code>this - that</code>.
187: */
188: public Float64 minus(Float64 that) {
189: Float64 r = FACTORY.object();
190: r._value = this ._value - that._value;
191: return r;
192: }
193:
194: /**
195: * Returns the difference between this number and the specified value.
196: *
197: * @param value the value to be subtracted.
198: * @return <code>this - value</code>.
199: */
200: public Float64 minus(double value) {
201: Float64 r = FACTORY.object();
202: r._value = this ._value - value;
203: return r;
204: }
205:
206: /**
207: * Returns the product of this number with the one specified.
208: *
209: * @param that the number multiplier.
210: * @return <code>this · that</code>.
211: */
212: public Float64 times(Float64 that) {
213: Float64 r = FACTORY.object();
214: r._value = this ._value * that._value;
215: return r;
216: }
217:
218: /**
219: * Returns the product of this number with the specified value.
220: *
221: * @param value the value multiplier.
222: * @return <code>this · value</code>.
223: */
224: public Float64 times(double value) {
225: Float64 r = FACTORY.object();
226: r._value = this ._value * value;
227: return r;
228: }
229:
230: /**
231: * Returns the reciprocal of this number.
232: *
233: * @return <code>1 / this</code>.
234: */
235: public Float64 inverse() {
236: Float64 r = FACTORY.object();
237: r._value = 1.0 / this ._value;
238: return r;
239: }
240:
241: /**
242: * Returns this number divided by the one specified.
243: *
244: * @param that the number divisor.
245: * @return <code>this / that</code>.
246: */
247: public Float64 divide(Float64 that) {
248: Float64 r = FACTORY.object();
249: r._value = this ._value / that._value;
250: return r;
251: }
252:
253: /**
254: * Returns this number divided by the specified value.
255: *
256: * @param value the value divisor.
257: * @return <code>this / value</code>.
258: */
259: public Float64 divide(double value) {
260: Float64 r = FACTORY.object();
261: r._value = this ._value / value;
262: return r;
263: }
264:
265: /**
266: * Compares the absolute value of this number with that number.
267: *
268: * @param that the number to compare with.
269: * @return <code>|this| > |that|</code>
270: */
271: public boolean isLargerThan(Float64 that) {
272: return MathLib.abs(this ._value) > MathLib.abs(that._value);
273: }
274:
275: /**
276: * Returns the absolute value of this number.
277: *
278: * @return <code>|this|</code>.
279: */
280: public Float64 abs() {
281: Float64 r = FACTORY.object();
282: r._value = MathLib.abs(this ._value);
283: return r;
284: }
285:
286: /**
287: * Returns the positive square root of this number.
288: *
289: * @return <code>sqrt(this)</code>.
290: */
291: public Float64 sqrt() {
292: Float64 r = FACTORY.object();
293: r._value = MathLib.sqrt(this ._value);
294: return r;
295: }
296:
297: /**
298: * Returns the exponential number <i>e</i> raised to the power of this
299: * number.
300: *
301: * @return <code>exp(this)</code>.
302: */
303: public Float64 exp() {
304: Float64 r = FACTORY.object();
305: r._value = MathLib.exp(this ._value);
306: return r;
307: }
308:
309: /**
310: * Returns the natural logarithm (base e) of this number.
311: *
312: * @return <code>log(this)</code>.
313: */
314: public Float64 log() {
315: Float64 r = FACTORY.object();
316: r._value = MathLib.log(this ._value);
317: return r;
318: }
319:
320: /**
321: * Returns this number raised to the power of the specified exponent.
322: *
323: * @param that the exponent.
324: * @return <code>this**that</code>.
325: */
326: public Float64 pow(Float64 that) {
327: Float64 r = FACTORY.object();
328: r._value = MathLib.pow(this ._value, that._value);
329: return r;
330: }
331:
332: /**
333: * Returns this number raised to the specified power.
334: *
335: * @param e the exponent.
336: * @return <code>this**e</code>.
337: */
338: public Float64 pow(double e) {
339: Float64 r = FACTORY.object();
340: r._value = MathLib.pow(this ._value, e);
341: return r;
342: }
343:
344: /**
345: * Returns the decimal text representation of this number.
346: *
347: * @return the text representation of this number.
348: */
349: public Text toText() {
350: return Text.valueOf(_value);
351: }
352:
353: /**
354: * Compares this number against the specified object.
355: *
356: * @param that the object to compare with.
357: * @return <code>true</code> if the objects are the same;
358: * <code>false</code> otherwise.
359: */
360: public boolean equals(Object that) {
361: return (that instanceof Float64)
362: && (this ._value == ((Float64) that)._value);
363: }
364:
365: /**
366: * Indicates if this number is equal to the specified value.
367: *
368: * @param value the value to compare with.
369: * @return <code>this.doubleValue() == value</code>.
370: */
371: public boolean equals(double value) {
372: return this ._value == value;
373: }
374:
375: /**
376: * Compares this number with the specified value for order.
377: *
378: * @param value the value to be compared with.
379: * @return a negative integer, zero, or a positive integer as this number
380: * is less than, equal to, or greater than the specified value.
381: */
382: public int compareTo(double value) {
383: if (this ._value < value) {
384: return -1;
385: } else if (this ._value > value) {
386: return 1;
387: } else {
388: long l1 = Double.doubleToLongBits(this ._value);
389: long l2 = Double.doubleToLongBits(value);
390: return (l1 == l2 ? 0 : (l1 < l2 ? -1 : 1));
391: }
392: }
393:
394: /**
395: * Returns the hash code for this number.
396: *
397: * @return the hash code value.
398: */
399: public int hashCode() {
400: int h = Float.floatToIntBits((float) _value);
401: h += ~(h << 9);
402: h ^= (h >>> 14);
403: h += (h << 4);
404: return h ^ (h >>> 10);
405: }
406:
407: @Override
408: public long longValue() {
409: return (long) _value;
410: }
411:
412: @Override
413: public double doubleValue() {
414: return _value;
415: }
416:
417: @Override
418: public int compareTo(Float64 that) {
419: return compareTo(that._value);
420: }
421:
422: @Override
423: public Float64 copy() {
424: return Float64.valueOf(_value);
425: }
426:
427: private static final long serialVersionUID = 1L;
428:
429: }
|