Browse Source

BigRational: parse nested fractionals from String (e.g. 1/2/3)

master
Joachim Klein 8 years ago
committed by Dave Parker
parent
commit
98d8e4cd72
  1. 18
      prism/src/param/BigRational.java

18
prism/src/param/BigRational.java

@ -193,10 +193,8 @@ public final class BigRational extends Number implements Comparable<BigRational>
this.den = new BigInteger("0"); this.den = new BigInteger("0");
return; return;
} }
BigInteger num;
BigInteger den;
string = string.trim(); string = string.trim();
int slashIdx = string.indexOf('/');
int slashIdx = string.lastIndexOf('/');
if (slashIdx < 0) { if (slashIdx < 0) {
// decimal point notation // decimal point notation
Double.parseDouble(string); // ensures correctness of format Double.parseDouble(string); // ensures correctness of format
@ -225,7 +223,8 @@ public final class BigRational extends Number implements Comparable<BigRational>
int eInt = Integer.parseInt(eStr); int eInt = Integer.parseInt(eStr);
expo += eInt; expo += eInt;
} }
num = new BigInteger((negate ? "-" : "") + noDotCoeff);
BigInteger num = new BigInteger((negate ? "-" : "") + noDotCoeff);
BigInteger den;
BigInteger ten = BITEN; BigInteger ten = BITEN;
if (expo == 0) { if (expo == 0) {
den = BigInteger.ONE; den = BigInteger.ONE;
@ -240,9 +239,14 @@ public final class BigRational extends Number implements Comparable<BigRational>
this.den = result.den; this.den = result.den;
} else { } else {
// fractional // fractional
num = new BigInteger(string.substring(0, slashIdx));
den = new BigInteger(string.substring(slashIdx + 1, string.length()));
BigRational r = cancel(num, den);
if (slashIdx == 0 || slashIdx == string.length()-1) {
throw new NumberFormatException("Illegal fraction syntax");
}
// because we use lastIndexOf, we obtain left-associativity,
// i.e. a/b/c is interpreted as (a/b)/c
BigRational num = new BigRational(string.substring(0, slashIdx));
BigRational den = new BigRational(string.substring(slashIdx + 1, string.length()));
BigRational r = num.divide(den);
this.num = r.num; this.num = r.num;
this.den = r.den; this.den = r.den;
return; return;

Loading…
Cancel
Save