KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > math > IntFraction


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 import java.io.*;
6
7 /** Implementation of exact rational numbers a ratio of two IntNums.
8  * @author Per Bothner
9  */

10
11 public class IntFraction extends RatNum implements Externalizable
12 {
13   IntNum num;
14   IntNum den;
15
16   IntFraction ()
17   {
18   }
19
20   public IntFraction (IntNum num, IntNum den)
21   {
22     this.num = num;
23     this.den = den;
24   }
25
26   public final IntNum numerator () { return num; }
27   public final IntNum denominator () { return den; }
28
29   public final boolean isNegative () { return num.isNegative (); }
30
31   public final int sign () { return num.sign (); }
32
33   public final int compare (Object JavaDoc obj)
34   {
35     if (obj instanceof RatNum)
36       return RatNum.compare (this, (RatNum) obj);
37     return ((RealNum)obj).compareReversed(this);
38   }
39
40   public int compareReversed (Numeric x)
41   {
42     return RatNum.compare ((RatNum) x, this);
43   }
44
45   public Numeric add (Object JavaDoc y, int k)
46   {
47     if (y instanceof RatNum)
48       return RatNum.add (this, (RatNum) y, k);
49     if (! (y instanceof Numeric))
50       throw new IllegalArgumentException JavaDoc ();
51     return ((Numeric)y).addReversed(this, k);
52   }
53
54   public Numeric addReversed (Numeric x, int k)
55   {
56     if (! (x instanceof RatNum))
57       throw new IllegalArgumentException JavaDoc ();
58     return RatNum.add ((RatNum)x, this, k);
59   }
60
61   public Numeric mul (Object JavaDoc y)
62   {
63     if (y instanceof RatNum)
64       return RatNum.times (this, (RatNum)y);
65     if (! (y instanceof Numeric))
66       throw new IllegalArgumentException JavaDoc ();
67     return ((Numeric)y).mulReversed(this);
68   }
69
70   public Numeric mulReversed (Numeric x)
71   {
72     if (! (x instanceof RatNum))
73       throw new IllegalArgumentException JavaDoc ();
74     return RatNum.times ((RatNum) x, this);
75   }
76
77   public Numeric div (Object JavaDoc y)
78   {
79     if (y instanceof RatNum)
80       return RatNum.divide (this, (RatNum)y);
81     if (! (y instanceof Numeric))
82       throw new IllegalArgumentException JavaDoc ();
83     return ((Numeric)y).divReversed(this);
84   }
85
86   public Numeric divReversed (Numeric x)
87   {
88     if (! (x instanceof RatNum))
89       throw new IllegalArgumentException JavaDoc ();
90     return RatNum.divide ((RatNum)x, this);
91   }
92
93   public static IntFraction neg (IntFraction x)
94   {
95     // If x is normalized, we do not need to call RatNum.make to normalize.
96
return new IntFraction (IntNum.neg (x.numerator()), x.denominator ());
97   }
98
99   public Numeric neg ()
100   {
101     return IntFraction.neg (this);
102   }
103
104   public long longValue ()
105   {
106     return toExactInt (ROUND).longValue ();
107   }
108
109   public double doubleValue ()
110   {
111     boolean neg = num.isNegative ();
112     if (den.isZero())
113       return (neg ? Double.NEGATIVE_INFINITY
114           : num.isZero() ? Double.NaN
115           : Double.POSITIVE_INFINITY);
116     IntNum n = num;
117     if (neg)
118       n = IntNum.neg (n);
119     int num_len = n.intLength ();
120     int den_len = den.intLength ();
121     int exp = 0;
122     if (num_len < den_len + 54)
123       {
124     exp = den_len + 54 - num_len;
125     n = IntNum.shift (n, exp);
126     exp = - exp;
127       }
128
129     // Divide n (which is shifted num) by den, using truncating division,
130
// and return quot and remainder.
131
IntNum quot = new IntNum ();
132     IntNum remainder = new IntNum ();
133     IntNum.divide (n, den, quot, remainder, TRUNCATE);
134     quot = quot.canonicalize ();
135     remainder = remainder.canonicalize ();
136
137     return quot.roundToDouble (exp, neg, !remainder.isZero ());
138   }
139
140   public String JavaDoc toString (int radix)
141   {
142     return num.toString(radix) + '/' + den.toString(radix);
143   }
144
145   /**
146    * @serialData Write the (canonicalized) numerator and denominator IntNums.
147    */

148   public void writeExternal(ObjectOutput out) throws IOException
149   {
150     out.writeObject(num);
151     out.writeObject(den);
152   }
153
154   /**
155    * @serialData Read the numerator and denominator as IntNums.
156    * Assumes they have no common factors.
157    */

158   public void readExternal(ObjectInput in)
159     throws IOException, ClassNotFoundException JavaDoc
160   {
161     num = (IntNum) in.readObject();
162     den = (IntNum) in.readObject();
163   }
164 }
165
Popular Tags