You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2019/01/18 22:55:45 UTC
[commons-numbers] branch fraction-dev updated: NUMBERS-91:
Conforming Big Fraction related Test classes
This is an automated email from the ASF dual-hosted git repository.
ericbarnhill pushed a commit to branch fraction-dev
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/fraction-dev by this push:
new 5a7d463 NUMBERS-91: Conforming Big Fraction related Test classes
5a7d463 is described below
commit 5a7d463806474aa23196225c3361e0d410deda40
Author: Eric Barnhill <er...@protonmail.ch>
AuthorDate: Fri Jan 18 14:55:39 2019 -0800
NUMBERS-91: Conforming Big Fraction related Test classes
---
.../numbers/fraction/BigFractionFormatTest.java | 10 +-
.../commons/numbers/fraction/BigFractionTest.java | 373 ++++++++++-----------
2 files changed, 189 insertions(+), 194 deletions(-)
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java
index 7998986..1810fc6 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionFormatTest.java
@@ -45,7 +45,7 @@ public class BigFractionFormatTest {
@Test
public void testFormat() {
- BigFraction c = new BigFraction(1, 2);
+ BigFraction c = BigFraction.of(1, 2);
String expected = "1 / 2";
String actual = properFormat.format(c);
@@ -57,7 +57,7 @@ public class BigFractionFormatTest {
@Test
public void testFormatNegative() {
- BigFraction c = new BigFraction(-1, 2);
+ BigFraction c = BigFraction.of(-1, 2);
String expected = "-1 / 2";
String actual = properFormat.format(c);
@@ -69,7 +69,7 @@ public class BigFractionFormatTest {
@Test
public void testFormatZero() {
- BigFraction c = new BigFraction(0, 1);
+ BigFraction c = BigFraction.of(0, 1);
String expected = "0 / 1";
String actual = properFormat.format(c);
@@ -81,7 +81,7 @@ public class BigFractionFormatTest {
@Test
public void testFormatImproper() {
- BigFraction c = new BigFraction(5, 3);
+ BigFraction c = BigFraction.of(5, 3);
String actual = properFormat.format(c);
Assert.assertEquals("1 2 / 3", actual);
@@ -92,7 +92,7 @@ public class BigFractionFormatTest {
@Test
public void testFormatImproperNegative() {
- BigFraction c = new BigFraction(-5, 3);
+ BigFraction c = BigFraction.of(-5, 3);
String actual = properFormat.format(c);
Assert.assertEquals("-1 2 / 3", actual);
diff --git a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
index e17eb2a..d15d78f 100644
--- a/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
+++ b/commons-numbers-fraction/src/test/java/org/apache/commons/numbers/fraction/BigFractionTest.java
@@ -38,48 +38,48 @@ public class BigFractionTest {
@Test
public void testConstructor() {
- assertFraction(0, 1, new BigFraction(0, 1));
- assertFraction(0, 1, new BigFraction(0l, 2l));
- assertFraction(0, 1, new BigFraction(0, -1));
- assertFraction(1, 2, new BigFraction(1, 2));
- assertFraction(1, 2, new BigFraction(2, 4));
- assertFraction(-1, 2, new BigFraction(-1, 2));
- assertFraction(-1, 2, new BigFraction(1, -2));
- assertFraction(-1, 2, new BigFraction(-2, 4));
- assertFraction(-1, 2, new BigFraction(2, -4));
- assertFraction(11, 1, new BigFraction(11));
- assertFraction(11, 1, new BigFraction(11l));
- assertFraction(11, 1, new BigFraction(new BigInteger("11")));
-
- assertFraction(0, 1, new BigFraction(0.00000000000001, 1.0e-5, 100));
- assertFraction(2, 5, new BigFraction(0.40000000000001, 1.0e-5, 100));
- assertFraction(15, 1, new BigFraction(15.0000000000001, 1.0e-5, 100));
-
- Assert.assertEquals(0.00000000000001, new BigFraction(0.00000000000001).doubleValue(), 0.0);
- Assert.assertEquals(0.40000000000001, new BigFraction(0.40000000000001).doubleValue(), 0.0);
- Assert.assertEquals(15.0000000000001, new BigFraction(15.0000000000001).doubleValue(), 0.0);
- assertFraction(3602879701896487l, 9007199254740992l, new BigFraction(0.40000000000001));
- assertFraction(1055531162664967l, 70368744177664l, new BigFraction(15.0000000000001));
+ assertFraction(0, 1, BigFraction.of(0, 1));
+ assertFraction(0, 1, BigFraction.of(0l, 2l));
+ assertFraction(0, 1, BigFraction.of(0, -1));
+ assertFraction(1, 2, BigFraction.of(1, 2));
+ assertFraction(1, 2, BigFraction.of(2, 4));
+ assertFraction(-1, 2, BigFraction.of(-1, 2));
+ assertFraction(-1, 2, BigFraction.of(1, -2));
+ assertFraction(-1, 2, BigFraction.of(-2, 4));
+ assertFraction(-1, 2, BigFraction.of(2, -4));
+ assertFraction(11, 1, BigFraction.of(11));
+ assertFraction(11, 1, BigFraction.of(11l));
+ assertFraction(11, 1, BigFraction.of(new BigInteger("11")));
+
+ assertFraction(0, 1, BigFraction.from(0.00000000000001, 1.0e-5, 100));
+ assertFraction(2, 5, BigFraction.from(0.40000000000001, 1.0e-5, 100));
+ assertFraction(15, 1, BigFraction.from(15.0000000000001, 1.0e-5, 100));
+
+ Assert.assertEquals(0.00000000000001, BigFraction.from(0.00000000000001).doubleValue(), 0.0);
+ Assert.assertEquals(0.40000000000001, BigFraction.from(0.40000000000001).doubleValue(), 0.0);
+ Assert.assertEquals(15.0000000000001, BigFraction.from(15.0000000000001).doubleValue(), 0.0);
+ assertFraction(3602879701896487l, 9007199254740992l, BigFraction.from(0.40000000000001));
+ assertFraction(1055531162664967l, 70368744177664l, BigFraction.from(15.0000000000001));
try {
- new BigFraction(null, BigInteger.ONE);
+ BigFraction.of(null, BigInteger.ONE);
Assert.fail("Expecting NullPointerException");
} catch (NullPointerException npe) {
// expected
}
try {
- new BigFraction(BigInteger.ONE, null);
+ BigFraction.of(BigInteger.ONE, null);
Assert.fail("Expecting NullPointerException");
} catch (NullPointerException npe) {
// expected
}
try {
- new BigFraction(BigInteger.ONE, BigInteger.ZERO);
+ BigFraction.of(BigInteger.ONE, BigInteger.ZERO);
Assert.fail("Expecting ArithmeticException");
} catch (ArithmeticException ignored) {
// expected
}
try {
- new BigFraction(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000);
+ BigFraction.from(2.0 * Integer.MAX_VALUE, 1.0e-5, 100000);
Assert.fail("Expecting ArithmeticException");
} catch (ArithmeticException ignored) {
// expected
@@ -89,100 +89,100 @@ public class BigFractionTest {
@Test(expected=FractionException.class)
public void testGoldenRatio() {
// the golden ratio is notoriously a difficult number for continuous fraction
- new BigFraction((1 + Math.sqrt(5)) / 2, 1.0e-12, 25);
+ BigFraction.from((1 + Math.sqrt(5)) / 2, 1.0e-12, 25);
}
// MATH-179
@Test
public void testDoubleConstructor() throws Exception {
- assertFraction(1, 2, new BigFraction((double) 1 / (double) 2, 1.0e-5, 100));
- assertFraction(1, 3, new BigFraction((double) 1 / (double) 3, 1.0e-5, 100));
- assertFraction(2, 3, new BigFraction((double) 2 / (double) 3, 1.0e-5, 100));
- assertFraction(1, 4, new BigFraction((double) 1 / (double) 4, 1.0e-5, 100));
- assertFraction(3, 4, new BigFraction((double) 3 / (double) 4, 1.0e-5, 100));
- assertFraction(1, 5, new BigFraction((double) 1 / (double) 5, 1.0e-5, 100));
- assertFraction(2, 5, new BigFraction((double) 2 / (double) 5, 1.0e-5, 100));
- assertFraction(3, 5, new BigFraction((double) 3 / (double) 5, 1.0e-5, 100));
- assertFraction(4, 5, new BigFraction((double) 4 / (double) 5, 1.0e-5, 100));
- assertFraction(1, 6, new BigFraction((double) 1 / (double) 6, 1.0e-5, 100));
- assertFraction(5, 6, new BigFraction((double) 5 / (double) 6, 1.0e-5, 100));
- assertFraction(1, 7, new BigFraction((double) 1 / (double) 7, 1.0e-5, 100));
- assertFraction(2, 7, new BigFraction((double) 2 / (double) 7, 1.0e-5, 100));
- assertFraction(3, 7, new BigFraction((double) 3 / (double) 7, 1.0e-5, 100));
- assertFraction(4, 7, new BigFraction((double) 4 / (double) 7, 1.0e-5, 100));
- assertFraction(5, 7, new BigFraction((double) 5 / (double) 7, 1.0e-5, 100));
- assertFraction(6, 7, new BigFraction((double) 6 / (double) 7, 1.0e-5, 100));
- assertFraction(1, 8, new BigFraction((double) 1 / (double) 8, 1.0e-5, 100));
- assertFraction(3, 8, new BigFraction((double) 3 / (double) 8, 1.0e-5, 100));
- assertFraction(5, 8, new BigFraction((double) 5 / (double) 8, 1.0e-5, 100));
- assertFraction(7, 8, new BigFraction((double) 7 / (double) 8, 1.0e-5, 100));
- assertFraction(1, 9, new BigFraction((double) 1 / (double) 9, 1.0e-5, 100));
- assertFraction(2, 9, new BigFraction((double) 2 / (double) 9, 1.0e-5, 100));
- assertFraction(4, 9, new BigFraction((double) 4 / (double) 9, 1.0e-5, 100));
- assertFraction(5, 9, new BigFraction((double) 5 / (double) 9, 1.0e-5, 100));
- assertFraction(7, 9, new BigFraction((double) 7 / (double) 9, 1.0e-5, 100));
- assertFraction(8, 9, new BigFraction((double) 8 / (double) 9, 1.0e-5, 100));
- assertFraction(1, 10, new BigFraction((double) 1 / (double) 10, 1.0e-5, 100));
- assertFraction(3, 10, new BigFraction((double) 3 / (double) 10, 1.0e-5, 100));
- assertFraction(7, 10, new BigFraction((double) 7 / (double) 10, 1.0e-5, 100));
- assertFraction(9, 10, new BigFraction((double) 9 / (double) 10, 1.0e-5, 100));
- assertFraction(1, 11, new BigFraction((double) 1 / (double) 11, 1.0e-5, 100));
- assertFraction(2, 11, new BigFraction((double) 2 / (double) 11, 1.0e-5, 100));
- assertFraction(3, 11, new BigFraction((double) 3 / (double) 11, 1.0e-5, 100));
- assertFraction(4, 11, new BigFraction((double) 4 / (double) 11, 1.0e-5, 100));
- assertFraction(5, 11, new BigFraction((double) 5 / (double) 11, 1.0e-5, 100));
- assertFraction(6, 11, new BigFraction((double) 6 / (double) 11, 1.0e-5, 100));
- assertFraction(7, 11, new BigFraction((double) 7 / (double) 11, 1.0e-5, 100));
- assertFraction(8, 11, new BigFraction((double) 8 / (double) 11, 1.0e-5, 100));
- assertFraction(9, 11, new BigFraction((double) 9 / (double) 11, 1.0e-5, 100));
- assertFraction(10, 11, new BigFraction((double) 10 / (double) 11, 1.0e-5, 100));
+ assertFraction(1, 2, BigFraction.from((double) 1 / (double) 2, 1.0e-5, 100));
+ assertFraction(1, 3, BigFraction.from((double) 1 / (double) 3, 1.0e-5, 100));
+ assertFraction(2, 3, BigFraction.from((double) 2 / (double) 3, 1.0e-5, 100));
+ assertFraction(1, 4, BigFraction.from((double) 1 / (double) 4, 1.0e-5, 100));
+ assertFraction(3, 4, BigFraction.from((double) 3 / (double) 4, 1.0e-5, 100));
+ assertFraction(1, 5, BigFraction.from((double) 1 / (double) 5, 1.0e-5, 100));
+ assertFraction(2, 5, BigFraction.from((double) 2 / (double) 5, 1.0e-5, 100));
+ assertFraction(3, 5, BigFraction.from((double) 3 / (double) 5, 1.0e-5, 100));
+ assertFraction(4, 5, BigFraction.from((double) 4 / (double) 5, 1.0e-5, 100));
+ assertFraction(1, 6, BigFraction.from((double) 1 / (double) 6, 1.0e-5, 100));
+ assertFraction(5, 6, BigFraction.from((double) 5 / (double) 6, 1.0e-5, 100));
+ assertFraction(1, 7, BigFraction.from((double) 1 / (double) 7, 1.0e-5, 100));
+ assertFraction(2, 7, BigFraction.from((double) 2 / (double) 7, 1.0e-5, 100));
+ assertFraction(3, 7, BigFraction.from((double) 3 / (double) 7, 1.0e-5, 100));
+ assertFraction(4, 7, BigFraction.from((double) 4 / (double) 7, 1.0e-5, 100));
+ assertFraction(5, 7, BigFraction.from((double) 5 / (double) 7, 1.0e-5, 100));
+ assertFraction(6, 7, BigFraction.from((double) 6 / (double) 7, 1.0e-5, 100));
+ assertFraction(1, 8, BigFraction.from((double) 1 / (double) 8, 1.0e-5, 100));
+ assertFraction(3, 8, BigFraction.from((double) 3 / (double) 8, 1.0e-5, 100));
+ assertFraction(5, 8, BigFraction.from((double) 5 / (double) 8, 1.0e-5, 100));
+ assertFraction(7, 8, BigFraction.from((double) 7 / (double) 8, 1.0e-5, 100));
+ assertFraction(1, 9, BigFraction.from((double) 1 / (double) 9, 1.0e-5, 100));
+ assertFraction(2, 9, BigFraction.from((double) 2 / (double) 9, 1.0e-5, 100));
+ assertFraction(4, 9, BigFraction.from((double) 4 / (double) 9, 1.0e-5, 100));
+ assertFraction(5, 9, BigFraction.from((double) 5 / (double) 9, 1.0e-5, 100));
+ assertFraction(7, 9, BigFraction.from((double) 7 / (double) 9, 1.0e-5, 100));
+ assertFraction(8, 9, BigFraction.from((double) 8 / (double) 9, 1.0e-5, 100));
+ assertFraction(1, 10, BigFraction.from((double) 1 / (double) 10, 1.0e-5, 100));
+ assertFraction(3, 10, BigFraction.from((double) 3 / (double) 10, 1.0e-5, 100));
+ assertFraction(7, 10, BigFraction.from((double) 7 / (double) 10, 1.0e-5, 100));
+ assertFraction(9, 10, BigFraction.from((double) 9 / (double) 10, 1.0e-5, 100));
+ assertFraction(1, 11, BigFraction.from((double) 1 / (double) 11, 1.0e-5, 100));
+ assertFraction(2, 11, BigFraction.from((double) 2 / (double) 11, 1.0e-5, 100));
+ assertFraction(3, 11, BigFraction.from((double) 3 / (double) 11, 1.0e-5, 100));
+ assertFraction(4, 11, BigFraction.from((double) 4 / (double) 11, 1.0e-5, 100));
+ assertFraction(5, 11, BigFraction.from((double) 5 / (double) 11, 1.0e-5, 100));
+ assertFraction(6, 11, BigFraction.from((double) 6 / (double) 11, 1.0e-5, 100));
+ assertFraction(7, 11, BigFraction.from((double) 7 / (double) 11, 1.0e-5, 100));
+ assertFraction(8, 11, BigFraction.from((double) 8 / (double) 11, 1.0e-5, 100));
+ assertFraction(9, 11, BigFraction.from((double) 9 / (double) 11, 1.0e-5, 100));
+ assertFraction(10, 11, BigFraction.from((double) 10 / (double) 11, 1.0e-5, 100));
}
// MATH-181
@Test
public void testDigitLimitConstructor() throws Exception {
- assertFraction(2, 5, new BigFraction(0.4, 9));
- assertFraction(2, 5, new BigFraction(0.4, 99));
- assertFraction(2, 5, new BigFraction(0.4, 999));
+ assertFraction(2, 5, BigFraction.from(0.4, 9));
+ assertFraction(2, 5, BigFraction.from(0.4, 99));
+ assertFraction(2, 5, BigFraction.from(0.4, 999));
- assertFraction(3, 5, new BigFraction(0.6152, 9));
- assertFraction(8, 13, new BigFraction(0.6152, 99));
- assertFraction(510, 829, new BigFraction(0.6152, 999));
- assertFraction(769, 1250, new BigFraction(0.6152, 9999));
+ assertFraction(3, 5, BigFraction.from(0.6152, 9));
+ assertFraction(8, 13, BigFraction.from(0.6152, 99));
+ assertFraction(510, 829, BigFraction.from(0.6152, 999));
+ assertFraction(769, 1250, BigFraction.from(0.6152, 9999));
// MATH-996
- assertFraction(1, 2, new BigFraction(0.5000000001, 10));
+ assertFraction(1, 2, BigFraction.from(0.5000000001, 10));
}
// MATH-1029
@Test(expected=ArithmeticException.class)
public void testPositiveValueOverflow() {
- assertFraction((long) 1e10, 1, new BigFraction(1e10, 1000));
+ assertFraction((long) 1e10, 1, BigFraction.from(1e10, 1000));
}
// MATH-1029
@Test(expected=ArithmeticException.class)
public void testNegativeValueOverflow() {
- assertFraction((long) -1e10, 1, new BigFraction(-1e10, 1000));
+ assertFraction((long) -1e10, 1, BigFraction.from(-1e10, 1000));
}
@Test
public void testEpsilonLimitConstructor() throws Exception {
- assertFraction(2, 5, new BigFraction(0.4, 1.0e-5, 100));
+ assertFraction(2, 5, BigFraction.from(0.4, 1.0e-5, 100));
- assertFraction(3, 5, new BigFraction(0.6152, 0.02, 100));
- assertFraction(8, 13, new BigFraction(0.6152, 1.0e-3, 100));
- assertFraction(251, 408, new BigFraction(0.6152, 1.0e-4, 100));
- assertFraction(251, 408, new BigFraction(0.6152, 1.0e-5, 100));
- assertFraction(510, 829, new BigFraction(0.6152, 1.0e-6, 100));
- assertFraction(769, 1250, new BigFraction(0.6152, 1.0e-7, 100));
+ assertFraction(3, 5, BigFraction.from(0.6152, 0.02, 100));
+ assertFraction(8, 13, BigFraction.from(0.6152, 1.0e-3, 100));
+ assertFraction(251, 408, BigFraction.from(0.6152, 1.0e-4, 100));
+ assertFraction(251, 408, BigFraction.from(0.6152, 1.0e-5, 100));
+ assertFraction(510, 829, BigFraction.from(0.6152, 1.0e-6, 100));
+ assertFraction(769, 1250, BigFraction.from(0.6152, 1.0e-7, 100));
}
@Test
public void testCompareTo() {
- BigFraction first = new BigFraction(1, 2);
- BigFraction second = new BigFraction(1, 3);
- BigFraction third = new BigFraction(1, 2);
+ BigFraction first = BigFraction.of(1, 2);
+ BigFraction second = BigFraction.of(1, 3);
+ BigFraction third = BigFraction.of(1, 2);
Assert.assertEquals(0, first.compareTo(first));
Assert.assertEquals(0, first.compareTo(third));
@@ -192,8 +192,8 @@ public class BigFractionTest {
// these two values are different approximations of PI
// the first one is approximately PI - 3.07e-18
// the second one is approximately PI + 1.936e-17
- BigFraction pi1 = new BigFraction(1068966896, 340262731);
- BigFraction pi2 = new BigFraction( 411557987, 131002976);
+ BigFraction pi1 = BigFraction.of(1068966896, 340262731);
+ BigFraction pi2 = BigFraction.of( 411557987, 131002976);
Assert.assertEquals(-1, pi1.compareTo(pi2));
Assert.assertEquals( 1, pi2.compareTo(pi1));
Assert.assertEquals(0.0, pi1.doubleValue() - pi2.doubleValue(), 1.0e-20);
@@ -202,8 +202,8 @@ public class BigFractionTest {
@Test
public void testDoubleValue() {
- BigFraction first = new BigFraction(1, 2);
- BigFraction second = new BigFraction(1, 3);
+ BigFraction first = BigFraction.of(1, 2);
+ BigFraction second = BigFraction.of(1, 3);
Assert.assertEquals(0.5, first.doubleValue(), 0.0);
Assert.assertEquals(1.0 / 3.0, second.doubleValue(), 0.0);
@@ -215,7 +215,7 @@ public class BigFractionTest {
final BigInteger pow400 = BigInteger.TEN.pow(400);
final BigInteger pow401 = BigInteger.TEN.pow(401);
final BigInteger two = new BigInteger("2");
- final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE),
+ final BigFraction large = BigFraction.of(pow401.add(BigInteger.ONE),
pow400.multiply(two));
Assert.assertEquals(5, large.doubleValue(), 1e-15);
@@ -227,7 +227,7 @@ public class BigFractionTest {
final BigInteger pow400 = BigInteger.TEN.pow(400);
final BigInteger pow401 = BigInteger.TEN.pow(401);
final BigInteger two = new BigInteger("2");
- final BigFraction large = new BigFraction(pow401.add(BigInteger.ONE),
+ final BigFraction large = BigFraction.of(pow401.add(BigInteger.ONE),
pow400.multiply(two));
Assert.assertEquals(5, large.floatValue(), 1e-15);
@@ -238,7 +238,7 @@ public class BigFractionTest {
public void testDoubleValueForLargeNumeratorAndSmallDenominator() {
final BigInteger pow300 = BigInteger.TEN.pow(300);
final BigInteger pow330 = BigInteger.TEN.pow(330);
- final BigFraction large = new BigFraction(pow330.add(BigInteger.ONE),
+ final BigFraction large = BigFraction.of(pow330.add(BigInteger.ONE),
pow300);
Assert.assertEquals(1e30, large.doubleValue(), 1e-15);
@@ -249,7 +249,7 @@ public class BigFractionTest {
public void testFloatValueForLargeNumeratorAndSmallDenominator() {
final BigInteger pow30 = BigInteger.TEN.pow(30);
final BigInteger pow40 = BigInteger.TEN.pow(40);
- final BigFraction large = new BigFraction(pow40.add(BigInteger.ONE),
+ final BigFraction large = BigFraction.of(pow40.add(BigInteger.ONE),
pow30);
Assert.assertEquals(1e10f, large.floatValue(), 1e-15);
@@ -257,8 +257,8 @@ public class BigFractionTest {
@Test
public void testFloatValue() {
- BigFraction first = new BigFraction(1, 2);
- BigFraction second = new BigFraction(1, 3);
+ BigFraction first = BigFraction.of(1, 2);
+ BigFraction second = BigFraction.of(1, 3);
Assert.assertEquals(0.5f, first.floatValue(), 0.0f);
Assert.assertEquals((float) (1.0 / 3.0), second.floatValue(), 0.0f);
@@ -266,8 +266,8 @@ public class BigFractionTest {
@Test
public void testIntValue() {
- BigFraction first = new BigFraction(1, 2);
- BigFraction second = new BigFraction(3, 2);
+ BigFraction first = BigFraction.of(1, 2);
+ BigFraction second = BigFraction.of(3, 2);
Assert.assertEquals(0, first.intValue());
Assert.assertEquals(1, second.intValue());
@@ -275,8 +275,8 @@ public class BigFractionTest {
@Test
public void testLongValue() {
- BigFraction first = new BigFraction(1, 2);
- BigFraction second = new BigFraction(3, 2);
+ BigFraction first = BigFraction.of(1, 2);
+ BigFraction second = BigFraction.of(3, 2);
Assert.assertEquals(0L, first.longValue());
Assert.assertEquals(1L, second.longValue());
@@ -284,32 +284,32 @@ public class BigFractionTest {
@Test
public void testConstructorDouble() {
- assertFraction(1, 2, new BigFraction(0.5));
- assertFraction(6004799503160661l, 18014398509481984l, new BigFraction(1.0 / 3.0));
- assertFraction(6124895493223875l, 36028797018963968l, new BigFraction(17.0 / 100.0));
- assertFraction(1784551352345559l, 562949953421312l, new BigFraction(317.0 / 100.0));
- assertFraction(-1, 2, new BigFraction(-0.5));
- assertFraction(-6004799503160661l, 18014398509481984l, new BigFraction(-1.0 / 3.0));
- assertFraction(-6124895493223875l, 36028797018963968l, new BigFraction(17.0 / -100.0));
- assertFraction(-1784551352345559l, 562949953421312l, new BigFraction(-317.0 / 100.0));
+ assertFraction(1, 2, BigFraction.from(0.5));
+ assertFraction(6004799503160661l, 18014398509481984l, BigFraction.from(1.0 / 3.0));
+ assertFraction(6124895493223875l, 36028797018963968l, BigFraction.from(17.0 / 100.0));
+ assertFraction(1784551352345559l, 562949953421312l, BigFraction.from(317.0 / 100.0));
+ assertFraction(-1, 2, BigFraction.from(-0.5));
+ assertFraction(-6004799503160661l, 18014398509481984l, BigFraction.from(-1.0 / 3.0));
+ assertFraction(-6124895493223875l, 36028797018963968l, BigFraction.from(17.0 / -100.0));
+ assertFraction(-1784551352345559l, 562949953421312l, BigFraction.from(-317.0 / 100.0));
for (double v : new double[] { Double.NaN, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY}) {
try {
- new BigFraction(v);
+ BigFraction.from(v);
Assert.fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException iae) {
// expected
}
}
- Assert.assertEquals(1l, new BigFraction(Double.MAX_VALUE).getDenominatorAsLong());
- Assert.assertEquals(1l, new BigFraction(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong());
- Assert.assertEquals(1l, new BigFraction(Double.MIN_VALUE).getNumeratorAsLong());
+ Assert.assertEquals(1l, BigFraction.from(Double.MAX_VALUE).getDenominatorAsLong());
+ Assert.assertEquals(1l, BigFraction.from(Double.longBitsToDouble(0x0010000000000000L)).getNumeratorAsLong());
+ Assert.assertEquals(1l, BigFraction.from(Double.MIN_VALUE).getNumeratorAsLong());
}
@Test
public void testAbs() {
- BigFraction a = new BigFraction(10, 21);
- BigFraction b = new BigFraction(-10, 21);
- BigFraction c = new BigFraction(10, -21);
+ BigFraction a = BigFraction.of(10, 21);
+ BigFraction b = BigFraction.of(-10, 21);
+ BigFraction c = BigFraction.of(10, -21);
assertFraction(10, 21, a.abs());
assertFraction(10, 21, b.abs());
@@ -320,22 +320,22 @@ public class BigFractionTest {
public void testReciprocal() {
BigFraction f = null;
- f = new BigFraction(50, 75);
+ f = BigFraction.of(50, 75);
f = f.reciprocal();
Assert.assertEquals(3, f.getNumeratorAsInt());
Assert.assertEquals(2, f.getDenominatorAsInt());
- f = new BigFraction(4, 3);
+ f = BigFraction.of(4, 3);
f = f.reciprocal();
Assert.assertEquals(3, f.getNumeratorAsInt());
Assert.assertEquals(4, f.getDenominatorAsInt());
- f = new BigFraction(-15, 47);
+ f = BigFraction.of(-15, 47);
f = f.reciprocal();
Assert.assertEquals(-47, f.getNumeratorAsInt());
Assert.assertEquals(15, f.getDenominatorAsInt());
- f = new BigFraction(0, 3);
+ f = BigFraction.of(0, 3);
try {
f = f.reciprocal();
Assert.fail("expecting ArithmeticException");
@@ -343,7 +343,7 @@ public class BigFractionTest {
}
// large values
- f = new BigFraction(Integer.MAX_VALUE, 1);
+ f = BigFraction.of(Integer.MAX_VALUE, 1);
f = f.reciprocal();
Assert.assertEquals(1, f.getNumeratorAsInt());
Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
@@ -353,18 +353,18 @@ public class BigFractionTest {
public void testNegate() {
BigFraction f = null;
- f = new BigFraction(50, 75);
+ f = BigFraction.of(50, 75);
f = f.negate();
Assert.assertEquals(-2, f.getNumeratorAsInt());
Assert.assertEquals(3, f.getDenominatorAsInt());
- f = new BigFraction(-50, 75);
+ f = BigFraction.of(-50, 75);
f = f.negate();
Assert.assertEquals(2, f.getNumeratorAsInt());
Assert.assertEquals(3, f.getDenominatorAsInt());
// large values
- f = new BigFraction(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
+ f = BigFraction.of(Integer.MAX_VALUE - 1, Integer.MAX_VALUE);
f = f.negate();
Assert.assertEquals(Integer.MIN_VALUE + 2, f.getNumeratorAsInt());
Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
@@ -373,22 +373,22 @@ public class BigFractionTest {
@Test
public void testAdd() {
- BigFraction a = new BigFraction(1, 2);
- BigFraction b = new BigFraction(2, 3);
+ BigFraction a = BigFraction.of(1, 2);
+ BigFraction b = BigFraction.of(2, 3);
assertFraction(1, 1, a.add(a));
assertFraction(7, 6, a.add(b));
assertFraction(7, 6, b.add(a));
assertFraction(4, 3, b.add(b));
- BigFraction f1 = new BigFraction(Integer.MAX_VALUE - 1, 1);
+ BigFraction f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1);
BigFraction f2 = BigFraction.ONE;
BigFraction f = f1.add(f2);
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
- f1 = new BigFraction(-1, 13 * 13 * 2 * 2);
- f2 = new BigFraction(-2, 13 * 17 * 2);
+ f1 = BigFraction.of(-1, 13 * 13 * 2 * 2);
+ f2 = BigFraction.of(-2, 13 * 17 * 2);
f = f1.add(f2);
Assert.assertEquals(13 * 13 * 17 * 2 * 2, f.getDenominatorAsInt());
Assert.assertEquals(-17 - 2 * 13 * 2, f.getNumeratorAsInt());
@@ -401,19 +401,19 @@ public class BigFractionTest {
// if this fraction is added naively, it will overflow.
// check that it doesn't.
- f1 = new BigFraction(1, 32768 * 3);
- f2 = new BigFraction(1, 59049);
+ f1 = BigFraction.of(1, 32768 * 3);
+ f2 = BigFraction.of(1, 59049);
f = f1.add(f2);
Assert.assertEquals(52451, f.getNumeratorAsInt());
Assert.assertEquals(1934917632, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MIN_VALUE, 3);
- f2 = new BigFraction(1, 3);
+ f1 = BigFraction.of(Integer.MIN_VALUE, 3);
+ f2 = BigFraction.of(1, 3);
f = f1.add(f2);
Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt());
Assert.assertEquals(3, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MAX_VALUE - 1, 1);
+ f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1);
f = f1.add(BigInteger.ONE);
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
@@ -422,7 +422,7 @@ public class BigFractionTest {
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MAX_VALUE - 1, 1);
+ f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1);
f = f1.add(1);
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
@@ -431,7 +431,7 @@ public class BigFractionTest {
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MAX_VALUE - 1, 1);
+ f1 = BigFraction.of(Integer.MAX_VALUE - 1, 1);
f = f1.add(1l);
Assert.assertEquals(Integer.MAX_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
@@ -444,15 +444,15 @@ public class BigFractionTest {
@Test
public void testDivide() {
- BigFraction a = new BigFraction(1, 2);
- BigFraction b = new BigFraction(2, 3);
+ BigFraction a = BigFraction.of(1, 2);
+ BigFraction b = BigFraction.of(2, 3);
assertFraction(1, 1, a.divide(a));
assertFraction(3, 4, a.divide(b));
assertFraction(4, 3, b.divide(a));
assertFraction(1, 1, b.divide(b));
- BigFraction f1 = new BigFraction(3, 5);
+ BigFraction f1 = BigFraction.of(3, 5);
BigFraction f2 = BigFraction.ZERO;
try {
f1.divide(f2);
@@ -460,24 +460,24 @@ public class BigFractionTest {
} catch (ArithmeticException ex) {
}
- f1 = new BigFraction(0, 5);
- f2 = new BigFraction(2, 7);
+ f1 = BigFraction.of(0, 5);
+ f2 = BigFraction.of(2, 7);
BigFraction f = f1.divide(f2);
Assert.assertSame(BigFraction.ZERO, f);
- f1 = new BigFraction(2, 7);
+ f1 = BigFraction.of(2, 7);
f2 = BigFraction.ONE;
f = f1.divide(f2);
Assert.assertEquals(2, f.getNumeratorAsInt());
Assert.assertEquals(7, f.getDenominatorAsInt());
- f1 = new BigFraction(1, Integer.MAX_VALUE);
+ f1 = BigFraction.of(1, Integer.MAX_VALUE);
f = f1.divide(f1);
Assert.assertEquals(1, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
- f2 = new BigFraction(1, Integer.MAX_VALUE);
+ f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ f2 = BigFraction.of(1, Integer.MAX_VALUE);
f = f1.divide(f2);
Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
@@ -488,17 +488,17 @@ public class BigFractionTest {
} catch (NullPointerException ex) {
}
- f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
f = f1.divide(BigInteger.valueOf(Integer.MIN_VALUE));
Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
Assert.assertEquals(1, f.getNumeratorAsInt());
- f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
f = f1.divide(Integer.MIN_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
Assert.assertEquals(1, f.getNumeratorAsInt());
- f1 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ f1 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
f = f1.divide((long) Integer.MIN_VALUE);
Assert.assertEquals(Integer.MAX_VALUE, f.getDenominatorAsInt());
Assert.assertEquals(1, f.getNumeratorAsInt());
@@ -507,16 +507,16 @@ public class BigFractionTest {
@Test
public void testMultiply() {
- BigFraction a = new BigFraction(1, 2);
- BigFraction b = new BigFraction(2, 3);
+ BigFraction a = BigFraction.of(1, 2);
+ BigFraction b = BigFraction.of(2, 3);
assertFraction(1, 4, a.multiply(a));
assertFraction(1, 3, a.multiply(b));
assertFraction(1, 3, b.multiply(a));
assertFraction(4, 9, b.multiply(b));
- BigFraction f1 = new BigFraction(Integer.MAX_VALUE, 1);
- BigFraction f2 = new BigFraction(Integer.MIN_VALUE, Integer.MAX_VALUE);
+ BigFraction f1 = BigFraction.of(Integer.MAX_VALUE, 1);
+ BigFraction f2 = BigFraction.of(Integer.MIN_VALUE, Integer.MAX_VALUE);
BigFraction f = f1.multiply(f2);
Assert.assertEquals(Integer.MIN_VALUE, f.getNumeratorAsInt());
Assert.assertEquals(1, f.getDenominatorAsInt());
@@ -539,15 +539,15 @@ public class BigFractionTest {
@Test
public void testSubtract() {
- BigFraction a = new BigFraction(1, 2);
- BigFraction b = new BigFraction(2, 3);
+ BigFraction a = BigFraction.of(1, 2);
+ BigFraction b = BigFraction.of(2, 3);
assertFraction(0, 1, a.subtract(a));
assertFraction(-1, 6, a.subtract(b));
assertFraction(1, 6, b.subtract(a));
assertFraction(0, 1, b.subtract(b));
- BigFraction f = new BigFraction(1, 1);
+ BigFraction f = BigFraction.of(1, 1);
try {
f.subtract((BigFraction) null);
Assert.fail("expecting NullPointerException");
@@ -556,19 +556,19 @@ public class BigFractionTest {
// if this fraction is subtracted naively, it will overflow.
// check that it doesn't.
- BigFraction f1 = new BigFraction(1, 32768 * 3);
- BigFraction f2 = new BigFraction(1, 59049);
+ BigFraction f1 = BigFraction.of(1, 32768 * 3);
+ BigFraction f2 = BigFraction.of(1, 59049);
f = f1.subtract(f2);
Assert.assertEquals(-13085, f.getNumeratorAsInt());
Assert.assertEquals(1934917632, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MIN_VALUE, 3);
- f2 = new BigFraction(1, 3).negate();
+ f1 = BigFraction.of(Integer.MIN_VALUE, 3);
+ f2 = BigFraction.of(1, 3).negate();
f = f1.subtract(f2);
Assert.assertEquals(Integer.MIN_VALUE + 1, f.getNumeratorAsInt());
Assert.assertEquals(3, f.getDenominatorAsInt());
- f1 = new BigFraction(Integer.MAX_VALUE, 1);
+ f1 = BigFraction.of(Integer.MAX_VALUE, 1);
f2 = BigFraction.ONE;
f = f1.subtract(f2);
Assert.assertEquals(Integer.MAX_VALUE - 1, f.getNumeratorAsInt());
@@ -578,30 +578,30 @@ public class BigFractionTest {
@Test
public void testBigDecimalValue() {
- Assert.assertEquals(new BigDecimal(0.5), new BigFraction(1, 2).bigDecimalValue());
- Assert.assertEquals(new BigDecimal("0.0003"), new BigFraction(3, 10000).bigDecimalValue());
- Assert.assertEquals(new BigDecimal("0"), new BigFraction(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN));
- Assert.assertEquals(new BigDecimal("0.333"), new BigFraction(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN));
+ Assert.assertEquals(new BigDecimal(0.5), BigFraction.of(1, 2).bigDecimalValue());
+ Assert.assertEquals(new BigDecimal("0.0003"), BigFraction.of(3, 10000).bigDecimalValue());
+ Assert.assertEquals(new BigDecimal("0"), BigFraction.of(1, 3).bigDecimalValue(BigDecimal.ROUND_DOWN));
+ Assert.assertEquals(new BigDecimal("0.333"), BigFraction.of(1, 3).bigDecimalValue(3, BigDecimal.ROUND_DOWN));
}
@Test
public void testEqualsAndHashCode() {
- BigFraction zero = new BigFraction(0, 1);
+ BigFraction zero = BigFraction.of(0, 1);
BigFraction nullFraction = null;
Assert.assertTrue(zero.equals(zero));
Assert.assertFalse(zero.equals(nullFraction));
Assert.assertFalse(zero.equals(Double.valueOf(0)));
- BigFraction zero2 = new BigFraction(0, 2);
+ BigFraction zero2 = BigFraction.of(0, 2);
Assert.assertTrue(zero.equals(zero2));
Assert.assertEquals(zero.hashCode(), zero2.hashCode());
- BigFraction one = new BigFraction(1, 1);
+ BigFraction one = BigFraction.of(1, 1);
Assert.assertFalse((one.equals(zero) || zero.equals(one)));
Assert.assertTrue(one.equals(BigFraction.ONE));
}
@Test
public void testGetReducedFraction() {
- BigFraction threeFourths = new BigFraction(3, 4);
+ BigFraction threeFourths = BigFraction.of(3, 4);
Assert.assertTrue(threeFourths.equals(BigFraction.getReducedFraction(6, 8)));
Assert.assertTrue(BigFraction.ZERO.equals(BigFraction.getReducedFraction(0, -1)));
try {
@@ -615,29 +615,24 @@ public class BigFractionTest {
}
@Test
- public void testPercentage() {
- Assert.assertEquals(50.0, new BigFraction(1, 2).percentageValue(), 1.0e-15);
- }
-
- @Test
public void testPow() {
- Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13));
- Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(13l));
- Assert.assertEquals(new BigFraction(8192, 1594323), new BigFraction(2, 3).pow(BigInteger.valueOf(13l)));
- Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0));
- Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(0l));
- Assert.assertEquals(BigFraction.ONE, new BigFraction(2, 3).pow(BigInteger.valueOf(0l)));
- Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13));
- Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(-13l));
- Assert.assertEquals(new BigFraction(1594323, 8192), new BigFraction(2, 3).pow(BigInteger.valueOf(-13l)));
+ Assert.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13));
+ Assert.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(13l));
+ Assert.assertEquals(BigFraction.of(8192, 1594323), BigFraction.of(2, 3).pow(BigInteger.valueOf(13l)));
+ Assert.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(0));
+ Assert.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(0l));
+ Assert.assertEquals(BigFraction.ONE, BigFraction.of(2, 3).pow(BigInteger.valueOf(0l)));
+ Assert.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13));
+ Assert.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(-13l));
+ Assert.assertEquals(BigFraction.of(1594323, 8192), BigFraction.of(2, 3).pow(BigInteger.valueOf(-13l)));
}
@Test
public void testMath340() {
- BigFraction fractionA = new BigFraction(0.00131);
- BigFraction fractionB = new BigFraction(.37).reciprocal();
+ BigFraction fractionA = BigFraction.from(0.00131);
+ BigFraction fractionB = BigFraction.from(.37).reciprocal();
BigFraction errorResult = fractionA.multiply(fractionB);
- BigFraction correctResult = new BigFraction(fractionA.getNumerator().multiply(fractionB.getNumerator()),
+ BigFraction correctResult = BigFraction.of(fractionA.getNumerator().multiply(fractionB.getNumerator()),
fractionA.getDenominator().multiply(fractionB.getDenominator()));
Assert.assertEquals(correctResult, errorResult);
}
@@ -645,9 +640,9 @@ public class BigFractionTest {
@Test
public void testSerial() {
BigFraction[] fractions = {
- new BigFraction(3, 4), BigFraction.ONE, BigFraction.ZERO,
- new BigFraction(17), new BigFraction(Math.PI, 1000),
- new BigFraction(-5, 2)
+ BigFraction.of(3, 4), BigFraction.ONE, BigFraction.ZERO,
+ BigFraction.of(17), BigFraction.from(Math.PI, 1000),
+ BigFraction.of(-5, 2)
};
for (BigFraction fraction : fractions) {
Assert.assertEquals(fraction, TestUtils.serializeAndRecover(fraction));