01: package fit;
02:
03: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
04: // Released under the terms of the GNU General Public License version 2 or later.
05:
06: // Warning: not (yet) a general number usable in all calculations.
07:
08: public class ScientificDouble extends Number implements Comparable {
09: protected double value;
10: protected double precision;
11:
12: public ScientificDouble(double value) {
13: this .value = value;
14: this .precision = 0;
15: }
16:
17: public static ScientificDouble valueOf(String s) {
18: ScientificDouble result = new ScientificDouble(Double
19: .parseDouble(s));
20: result.precision = precision(s);
21: return result;
22: }
23:
24: public static double precision(String s) {
25: double value = Double.parseDouble(s);
26: double bound = Double.parseDouble(tweak(s.trim()));
27: return Math.abs(bound - value);
28: }
29:
30: public static String tweak(String s) {
31: int pos;
32: if ((pos = s.toLowerCase().indexOf("e")) >= 0) {
33: return tweak(s.substring(0, pos)) + s.substring(pos);
34: }
35: if (s.indexOf(".") >= 0) {
36: return s + "5";
37: }
38: return s + ".5";
39: }
40:
41: public boolean equals(Object obj) {
42: return compareTo(obj) == 0;
43: }
44:
45: public int compareTo(Object obj) {
46: double other = ((Number) obj).doubleValue();
47: double diff = value - other;
48: // System.out.println(value+" "+precision+" "+diff);
49: if (diff < -precision)
50: return -1;
51: if (diff > precision)
52: return 1;
53: if (Double.isNaN(value) && Double.isNaN(other))
54: return 0;
55: if (Double.isNaN(value))
56: return 1;
57: if (Double.isNaN(other))
58: return -1;
59: return 0;
60: }
61:
62: public String toString() {
63: return Double.toString(value);
64: }
65:
66: public double doubleValue() {
67: return value;
68: }
69:
70: public float floatValue() {
71: return (float) value;
72: }
73:
74: public long longValue() {
75: return (long) value;
76: }
77:
78: public int intValue() {
79: return (int) value;
80: }
81: }
|