| 1 4 package gnu.math; 5 6 public abstract class Numeric extends java.lang.Number  7 { 8 public float floatValue () { return (float) doubleValue(); } 9 public int intValue() { return (int) longValue(); } 10 public long longValue() { return (long) doubleValue(); } 11 12 13 public abstract Numeric add (Object obj, int k); 14 15 public final Numeric add (Object obj) { return add (obj, 1); } 16 public final Numeric sub (Object obj) { return add (obj, -1); } 17 18 public abstract Numeric mul (Object obj); 19 20 public abstract Numeric div (Object obj); 21 22 public abstract Numeric abs (); 23 24 public abstract Numeric neg (); 25 26 public abstract String toString (int radix); 27 28 public String toString () { return toString (10); } 29 30 public abstract boolean isExact (); 31 32 public abstract boolean isZero (); 33 34 35 public static final int FLOOR = 1; 36 public static final int CEILING = 2; 37 public static final int TRUNCATE = 3; 38 public static final int ROUND = 4; 39 40 45 public int compare (Object obj) 46 { 47 return -3; 48 } 49 50 public int compareReversed (Numeric x) 51 { 52 throw new IllegalArgumentException (); 53 } 54 55 public boolean equals (Object obj) 56 { 57 if (obj == null || ! (obj instanceof Numeric)) 58 return false; 59 return compare (obj) == 0; 60 } 61 62 public boolean grt (Object x) 63 { 64 return compare (x) > 0; 65 } 66 67 public boolean geq (Object x) 68 { 69 return compare (x) >= 0; 70 } 71 72 73 public Numeric addReversed (Numeric x, int k) 74 { 75 throw new IllegalArgumentException (); 76 } 77 78 public Numeric mulReversed (Numeric x) 79 { 80 throw new IllegalArgumentException (); 81 } 82 83 public Numeric divReversed (Numeric x) 84 { 85 throw new IllegalArgumentException (); 86 } 87 88 89 public Numeric div_inv () 90 { 91 return IntNum.one().div(this); 92 } 93 94 95 public Numeric mul_ident () 96 { 97 return IntNum.one(); 98 } 99 100 103 public Numeric power (IntNum y) 104 { 105 if (y.isNegative ()) 106 return power(IntNum.neg(y)).div_inv(); 107 Numeric pow2 = this; 108 Numeric r = null; 109 for (;;) { 111 if (y.isOdd()) 114 r = r == null ? pow2 : r.mul (pow2); y = IntNum.shift (y, -1); 116 if (y.isZero()) 117 break; 118 pow2 = pow2.mul (pow2); 120 } 121 return r == null ? mul_ident() : r; 122 } 123 124 } 125 | Popular Tags |