-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigRational.java
More file actions
59 lines (45 loc) · 1.53 KB
/
BigRational.java
File metadata and controls
59 lines (45 loc) · 1.53 KB
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
package de.ertlu.rob.MathEx;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
public class BigRational {
public static final BigInteger DEFAULT_MAXIMUM_DENOMINATOR = BigInteger.valueOf(1000000);
public final BigInteger numerator;
public final BigInteger denominator;
public BigRational(BigDecimal approximateValue) {
this(approximateValue, DEFAULT_MAXIMUM_DENOMINATOR);
}
public BigRational(BigDecimal approximateValue, BigInteger maximumDenominator) {
BigInteger m00 = BigInteger.ONE;
BigInteger m01 = BigInteger.ZERO;
BigInteger m10 = BigInteger.ZERO;
BigInteger m11 = BigInteger.ONE;
BigDecimal x = approximateValue;
BigInteger xRoundedInt, temp;
BigDecimal xRoundedDec;
BigInteger n = m00;
BigInteger d = m10;
while ( m10.multiply(xRoundedInt = x.setScale(0, RoundingMode.HALF_EVEN).toBigInteger()).add(m11).compareTo(maximumDenominator) <= 0) {
xRoundedDec = new BigDecimal(xRoundedInt);
temp = m00.multiply(xRoundedInt).add(m01);
m01 = m00;
m00 = temp;
temp = m10.multiply(xRoundedInt).add(m11);
m11 = m10;
m10 = temp;
if ( x.compareTo(xRoundedDec) == 0 ) break;
x = BigDecimal.ONE.divide(x.subtract(xRoundedDec), 34, RoundingMode.HALF_EVEN);
}
n = m00;
d = m10;
if ( d.compareTo(BigInteger.ZERO) < 0 ) {
n = n.multiply(BigInteger.valueOf(-1));
d = d.multiply(BigInteger.valueOf(-1));
}
numerator = n;
denominator = d;
}
public String toString() {
return "(" + numerator + "/" + denominator + ")";
}
}