KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > Numeric


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.math;
5
6 public abstract class Numeric extends java.lang.Number JavaDoc
7 {
8   public float floatValue () { return (float) doubleValue(); }
9   public int intValue() { return (int) longValue(); }
10   public long longValue() { return (long) doubleValue(); }
11
12   /** Return this + k * obj. */
13   public abstract Numeric add (Object JavaDoc obj, int k);
14
15   public final Numeric add (Object JavaDoc obj) { return add (obj, 1); }
16   public final Numeric sub (Object JavaDoc obj) { return add (obj, -1); }
17
18   public abstract Numeric mul (Object JavaDoc obj);
19
20   public abstract Numeric div (Object JavaDoc obj);
21
22   public abstract Numeric abs ();
23
24   public abstract Numeric neg ();
25
26   public abstract String JavaDoc toString (int radix);
27
28   public String JavaDoc toString () { return toString (10); }
29
30   public abstract boolean isExact ();
31
32   public abstract boolean isZero ();
33
34   /* Rounding modes: */
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   /** Return an integer for which of {# code this} or {#code obj} is larger.
41    * Return 1 if {@code this>obj}; 0 if {@code this==obj};
42    * -1 if {@code this<obj};
43    * -2 if {@code this!=obj} otherwise (for example if either is NaN);
44    * -3 if not comparable (incompatible types). */

45   public int compare (Object JavaDoc obj)
46   {
47     return -3;
48   }
49
50   public int compareReversed (Numeric x)
51   {
52     throw new IllegalArgumentException JavaDoc ();
53   }
54
55   public boolean equals (Object JavaDoc obj)
56   {
57     if (obj == null || ! (obj instanceof Numeric))
58       return false;
59     return compare (obj) == 0;
60   }
61
62   public boolean grt (Object JavaDoc x)
63   {
64     return compare (x) > 0;
65   }
66
67   public boolean geq (Object JavaDoc x)
68   {
69     return compare (x) >= 0;
70   }
71
72   /** Calculate x+k&this. */
73   public Numeric addReversed (Numeric x, int k)
74   {
75     throw new IllegalArgumentException JavaDoc ();
76   }
77
78   public Numeric mulReversed (Numeric x)
79   {
80     throw new IllegalArgumentException JavaDoc ();
81   }
82
83   public Numeric divReversed (Numeric x)
84   {
85     throw new IllegalArgumentException JavaDoc ();
86   }
87
88   /** Return the multiplicative inverse. */
89   public Numeric div_inv ()
90   {
91     return IntNum.one().div(this);
92   }
93
94   /** Return the multiplicative identity. */
95   public Numeric mul_ident ()
96   {
97     return IntNum.one();
98   }
99
100   /** Return this raised to an integer power.
101    * Implemented by repeated squaring and multiplication.
102    * If y < 0, returns div_inv of the result. */

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 (;;) // for (i = 0; ; i++)
110
{
111     // pow2 == x**(2**i)
112
// prod = x**(sum(j=0..i-1, (y>>j)&1))
113
if (y.isOdd())
114       r = r == null ? pow2 : r.mul (pow2); // r *= pow2
115
y = IntNum.shift (y, -1);
116     if (y.isZero())
117       break;
118     // pow2 *= pow2;
119
pow2 = pow2.mul (pow2);
120       }
121     return r == null ? mul_ident() : r;
122   }
123
124 }
125
Popular Tags