1 4 package gnu.math; 5 import java.io.*; 6 7 public class DFloNum extends RealNum implements Externalizable 8 { 9 double value; 10 11 public DFloNum () 12 { 13 } 14 15 public DFloNum (double value) 16 { 17 this.value = value; 18 } 19 20 public DFloNum (String s) throws NumberFormatException 21 { 22 Double d = Double.valueOf (s); value = d.doubleValue (); 24 25 32 36 if (value == 0.0 && s.charAt (0) == '-') 38 value = -0.0; 39 } 40 41 public static DFloNum make (double value) 42 { 43 return new DFloNum (value); 44 } 45 46 public final double doubleValue () 47 { 48 return value; 49 } 50 51 public long longValue () 52 { 53 return (long) value; 54 } 55 56 public int hashCode () 57 { 58 return (int)value; 59 } 60 61 public boolean equals (Object obj) 62 { 63 return (obj != null) 65 && (obj instanceof DFloNum) 66 && (Double.doubleToLongBits(((DFloNum)obj).value) 67 == Double.doubleToLongBits(value)); 68 } 69 70 public Numeric add (Object y, int k) 71 { 72 if (y instanceof RealNum) 73 return new DFloNum (value + k * ((RealNum)y).doubleValue ()); 74 if (!(y instanceof Numeric)) 75 throw new IllegalArgumentException (); 76 return ((Numeric)y).addReversed(this, k); 77 } 78 79 public Numeric addReversed (Numeric x, int k) 80 { 81 if (x instanceof RealNum) 82 return new DFloNum (((RealNum)x).doubleValue () + k * value); 83 throw new IllegalArgumentException (); 84 } 85 86 public Numeric mul (Object y) 87 { 88 if (y instanceof RealNum) 89 return new DFloNum (value * ((RealNum)y).doubleValue ()); 90 if (!(y instanceof Numeric)) 91 throw new IllegalArgumentException (); 92 return ((Numeric)y).mulReversed(this); 93 } 94 95 public Numeric mulReversed (Numeric x) 96 { 97 if (x instanceof RealNum) 98 return new DFloNum (((RealNum)x).doubleValue () * value); 99 throw new IllegalArgumentException (); 100 } 101 102 private static final DFloNum one = new DFloNum(1.0); 103 public static final DFloNum one() { return one; } 104 105 public Numeric div (Object y) 106 { 107 if (y instanceof RealNum) 108 return new DFloNum (value / ((RealNum)y).doubleValue ()); 109 if (!(y instanceof Numeric)) 110 throw new IllegalArgumentException (); 111 return ((Numeric)y).divReversed(this); 112 } 113 114 public Numeric divReversed (Numeric x) 115 { 116 if (x instanceof RealNum) 117 return new DFloNum (((RealNum)x).doubleValue () / value); 118 throw new IllegalArgumentException (); 119 } 120 121 public Numeric power (IntNum y) 122 { 123 return new DFloNum (Math.pow (doubleValue(), y.doubleValue())); 124 } 125 126 public boolean isNegative () 127 { 128 return value < 0; 129 } 130 131 public Numeric neg () 132 { 133 return new DFloNum (-value); 134 } 135 136 public int sign () 137 { 138 return value > 0.0 ? 1 : value < 0.0 ? -1 : value == 0.0 ? 0: -2; 139 } 140 141 public static int compare (double x, double y) 142 { 143 return x > y ? 1 : x < y ? -1 : x == y ? 0 : -2; 144 } 145 146 147 public static int compare(IntNum x_num, IntNum x_den, double y) 148 { 149 if (Double.isNaN (y)) 150 return -2; 151 if (Double.isInfinite (y)) 152 { 153 int result = y >= 0.0 ? -1 : 1; 154 if (! x_den.isZero()) 155 return result; if (x_num.isZero()) 157 return -2; result >>= 1; 159 return x_num.isNegative() ? result : ~result; 160 } 161 else 162 { 163 long bits = Double.doubleToLongBits (y); 164 boolean neg = bits < 0; 165 int exp = (int) (bits >> 52) & 0x7FF; 166 bits &= 0xfffffffffffffL; 167 if (exp == 0) 168 bits <<= 1; 169 else 170 bits |= 0x10000000000000L; 171 IntNum y_num = IntNum.make (neg ? -bits : bits); 172 if (exp >= 1075) 173 y_num = IntNum.shift (y_num, exp - 1075); 174 else 175 x_num = IntNum.shift (x_num, 1075 - exp); 176 return IntNum.compare (x_num, IntNum.times (y_num, x_den)); 177 } 178 } 179 180 public int compare (Object obj) 181 { 182 if (obj instanceof RatNum) 183 { 184 RatNum y_rat = (RatNum) obj; 185 int i = compare(y_rat.numerator(), y_rat.denominator(), value); 186 return i < -1 ? i : -i; 187 } 188 return compare (value, ((RealNum)obj).doubleValue ()); 189 } 190 191 public int compareReversed (Numeric x) 192 { 193 if (x instanceof RatNum) 194 { 195 RatNum x_rat = (RatNum) x; 196 return compare(x_rat.numerator(), x_rat.denominator(), value); 197 } 198 return compare (((RealNum)x).doubleValue (), value); 199 } 200 201 public boolean isExact () 202 { 203 return false; 204 } 205 206 public boolean isZero () 207 { 208 return value == 0.0; 209 } 210 211 212 public static RatNum toExact (double value) 213 { 214 if (Double.isInfinite (value)) 215 return RatNum.infinity(value >= 0.0 ? 1 : -1); 216 if (Double.isNaN (value)) 217 throw new ArithmeticException ("cannot convert NaN to exact rational"); 218 long bits = Double.doubleToLongBits (value); 219 boolean neg = bits < 0; 220 int exp = (int) (bits >> 52) & 0x7FF; 221 bits &= 0xfffffffffffffL; 222 if (exp == 0) 223 bits <<= 1; 224 else 225 bits |= 0x10000000000000L; 226 IntNum mant = IntNum.make (neg ? -bits : bits); 227 if (exp >= 1075) 228 return IntNum.shift (mant, exp - 1075); 229 else 230 return RatNum.make (mant, IntNum.shift (IntNum.one(), 1075 - exp)); 231 } 232 233 public String toString () 234 { 235 return (value == 1.0/0.0 ? "#i1/0" 236 : value == -1.0/0.0 ? "#i-1/0" 237 : Double.isNaN (value) ? "#i0/0" 238 : Double.toString (value)); 239 } 240 241 public String toString (int radix) 242 { 243 if (radix == 10) 244 return toString (); 245 return "#d" + toString (); 246 } 247 248 251 public void writeExternal(ObjectOutput out) throws IOException 252 { 253 out.writeDouble(value); 254 } 255 256 public void readExternal(ObjectInput in) 257 throws IOException, ClassNotFoundException 258 { 259 value = in.readDouble(); 260 } 261 262 341 } 342 | Popular Tags |