File: Fraction.java

package info (click to toggle)
concurrent-dfsg 1.3.4-6
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 976 kB
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (216 lines) | stat: -rw-r--r-- 5,763 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
  File: Fraction.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  History:
  Date       Who                What
  7Jul1998  dl               Create public version
  11Oct1999 dl               add hashCode
*/

package EDU.oswego.cs.dl.util.concurrent.misc;


/**
 * An immutable class representing fractions as pairs of longs.
 * Fractions are always maintained in reduced form.
 **/
public class Fraction implements Cloneable, Comparable, java.io.Serializable {
  protected final long numerator_;
  protected final long denominator_;

  /** Return the numerator **/
  public final long numerator() { return numerator_; }

  /** Return the denominator **/
  public final long denominator() { return denominator_; }

  /** Create a Fraction equal in value to num / den **/
  public Fraction(long num, long den) {
    // normalize while constructing
    boolean numNonnegative = (num >= 0);
    boolean denNonnegative = (den >= 0);
    long a = numNonnegative? num : -num;
    long b = denNonnegative? den : -den;
    long g = gcd(a, b);
    numerator_ = (numNonnegative == denNonnegative)? (a / g) : (-a / g);
    denominator_ = b / g;
  }

  /** Create a fraction with the same value as Fraction f **/
  public Fraction(Fraction f) {
    numerator_ = f.numerator();
    denominator_ = f.denominator();
  }

  public String toString() { 
    if (denominator() == 1) 
      return "" + numerator();
    else
      return numerator() + "/" + denominator(); 
  }

  public Object clone() { return new Fraction(this); }

  /** Return the value of the Fraction as a double **/
  public double asDouble() { 
    return ((double)(numerator())) / ((double)(denominator()));
  }

  /** 
   * Compute the nonnegative greatest common divisor of a and b.
   * (This is needed for normalizing Fractions, but can be
   * useful on its own.)
   **/
  public static long gcd(long a, long b) { 
    long x;
    long y;

    if (a < 0) a = -a;
    if (b < 0) b = -b;

    if (a >= b) { x = a; y = b; }
    else        { x = b; y = a; }

    while (y != 0) {
      long t = x % y;
      x = y;
      y = t;
    }
    return x;
  }

  /** return a Fraction representing the negated value of this Fraction **/
  public Fraction negative() {
    long an = numerator();
    long ad = denominator();
    return new Fraction(-an, ad);
  }

  /** return a Fraction representing 1 / this Fraction **/
  public Fraction inverse() {
    long an = numerator();
    long ad = denominator();
    return new Fraction(ad, an);
  }


  /** return a Fraction representing this Fraction plus b **/
  public Fraction plus(Fraction b) {
    long an = numerator();
    long ad = denominator();
    long bn = b.numerator();
    long bd = b.denominator();
    return new Fraction(an*bd+bn*ad, ad*bd);
  }

  /** return a Fraction representing this Fraction plus n **/
  public Fraction plus(long n) {
    long an = numerator();
    long ad = denominator();
    long bn = n;
    long bd = 1;
    return new Fraction(an*bd+bn*ad, ad*bd);
  }

  /** return a Fraction representing this Fraction minus b **/
  public Fraction minus(Fraction b) {
    long an = numerator();
    long ad = denominator();
    long bn = b.numerator();
    long bd = b.denominator();
    return new Fraction(an*bd-bn*ad, ad*bd);
  }

  /** return a Fraction representing this Fraction minus n **/
  public Fraction minus(long n) {
    long an = numerator();
    long ad = denominator();
    long bn = n;
    long bd = 1;
    return new Fraction(an*bd-bn*ad, ad*bd);
  }


  /** return a Fraction representing this Fraction times b **/
  public Fraction times(Fraction b) {
    long an = numerator();
    long ad = denominator();
    long bn = b.numerator();
    long bd = b.denominator();
    return new Fraction(an*bn, ad*bd);
  }

  /** return a Fraction representing this Fraction times n **/
  public Fraction times(long n) {
    long an = numerator();
    long ad = denominator();
    long bn = n;
    long bd = 1;
    return new Fraction(an*bn, ad*bd);
  }

  /** return a Fraction representing this Fraction divided by b **/
  public Fraction dividedBy(Fraction b) {
    long an = numerator();
    long ad = denominator();
    long bn = b.numerator();
    long bd = b.denominator();
    return new Fraction(an*bd, ad*bn);
  }

  /** return a Fraction representing this Fraction divided by n **/
  public Fraction dividedBy(long n) {
    long an = numerator();
    long ad = denominator();
    long bn = n;
    long bd = 1;
    return new Fraction(an*bd, ad*bn);
  }

  /** return a number less, equal, or greater than zero
   * reflecting whether this Fraction is less, equal or greater than 
   * the value of Fraction other.
   **/
  public int compareTo(Object other) {
    Fraction b = (Fraction)(other);
    long an = numerator();
    long ad = denominator();
    long bn = b.numerator();
    long bd = b.denominator();
    long l = an*bd;
    long r = bn*ad;
    return (l < r)? -1 : ((l == r)? 0: 1);
  }

  /** return a number less, equal, or greater than zero
   * reflecting whether this Fraction is less, equal or greater than n.
   **/

  public int compareTo(long n) {
    long an = numerator();
    long ad = denominator();
    long bn = n;
    long bd = 1;
    long l = an*bd;
    long r = bn*ad;
    return (l < r)? -1 : ((l == r)? 0: 1);
  }

  public boolean equals(Object other) {
    return compareTo((Fraction)other) == 0;
  }

  public boolean equals(long n) {
    return compareTo(n) == 0;
  }

  public int hashCode() {
    return (int) (numerator_ ^ denominator_);
  }

}