Skip to content

Commit 34092f5

Browse files
committed
Improvements to Rational handling
1 parent e8ba160 commit 34092f5

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

Source/com/drew/lang/Rational.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,6 @@ public String toSimpleString(boolean allowDecimal)
217217
return toString();
218218
} else if (isInteger()) {
219219
return Integer.toString(intValue());
220-
} else if (_numerator != 1 && _denominator % _numerator == 0) {
221-
// common factor between denominator and numerator
222-
long newDenominator = _denominator / _numerator;
223-
return new Rational(1, newDenominator).toSimpleString(allowDecimal);
224220
} else {
225221
Rational simplifiedInstance = getSimplifiedInstance();
226222
if (allowDecimal) {
@@ -314,9 +310,17 @@ public int hashCode()
314310
@NotNull
315311
public Rational getSimplifiedInstance()
316312
{
317-
long gcd = GCD(_numerator, _denominator);
313+
long n = _numerator;
314+
long d = _denominator;
318315

319-
return new Rational(_numerator / gcd, _denominator / gcd);
316+
if (d < 0) {
317+
n = -n;
318+
d = -d;
319+
}
320+
321+
long gcd = GCD(n, d);
322+
323+
return new Rational(n / gcd, d / gcd);
320324
}
321325

322326
private static long GCD(long a, long b)

Tests/com/drew/lang/RationalTest.java

+27-6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public void testToSimpleString() throws Exception
7272
Rational twoThirds = new Rational(10, 15);
7373
assertEquals("2/3", twoThirds.toSimpleString(true));
7474

75+
Rational twoSixths = new Rational(2, 6);
76+
assertEquals("1/3", twoSixths.toSimpleString(true));
77+
assertEquals("1/3", twoSixths.toSimpleString(false));
78+
7579
Rational two = new Rational(10, 5);
7680
assertTrue(two.isInteger());
7781
assertEquals("2", two.toSimpleString(true));
@@ -93,10 +97,6 @@ public void testToSimpleString() throws Exception
9397
assertTrue(zero.isInteger());
9498
assertEquals("0", zero.toSimpleString(true));
9599
assertEquals("0", zero.toSimpleString(false));
96-
97-
// not sure this is a nice presentation of rationals. won't implement it for now.
98-
// Rational twoAndAHalf = new Rational(10,4);
99-
// assertEquals("2 1/2", twoAndAHalf.toSimpleString());
100100
}
101101

102102
@Test
@@ -215,7 +215,7 @@ public void simplifiedInstances()
215215
assertEquals(actualSimple.doubleValue(), complex.doubleValue(), 0.0001);
216216
}
217217

218-
simple = new Rational(1, -2);
218+
simple = new Rational(-1, 2);
219219

220220
for (int prime : _primes)
221221
{
@@ -226,7 +226,7 @@ public void simplifiedInstances()
226226
assertEquals(actualSimple.doubleValue(), complex.doubleValue(), 0.0001);
227227
}
228228

229-
simple = new Rational(-1, -2);
229+
simple = new Rational(1, 2);
230230

231231
for (int prime : _primes)
232232
{
@@ -241,4 +241,25 @@ public void simplifiedInstances()
241241
assertEquals(new Rational(-32768, 32767), new Rational(-32768, 32767).getSimplifiedInstance());
242242
}
243243

244+
@Test
245+
public void getSimplifiedInstance_FlipsSignsIfNeeded()
246+
{
247+
Rational r = new Rational(1, -2);
248+
249+
Rational s = r.getSimplifiedInstance();
250+
251+
assertEquals(-1, s.getNumerator());
252+
assertEquals(2, s.getDenominator());
253+
}
254+
255+
@Test
256+
public void getSimplifiedInstance_RemovesSignsIfNeeded()
257+
{
258+
Rational r = new Rational(-1, -2);
259+
260+
Rational s = r.getSimplifiedInstance();
261+
262+
assertEquals(1, s.getNumerator());
263+
assertEquals(2, s.getDenominator());
264+
}
244265
}

0 commit comments

Comments
 (0)