public Fraction(BigDecimal original) { super(); double A, C, D, X, Y, M; C = original.doubleValue(); A = C; D = C; X = 1; Y = 0; do { M = Y; Y = X + Y * Math.round(C); X = M; if (C != Math.round(C)) { C = 1 / (C - Math.round(C)); } M = Math.round(D * Y); } while (M != MathUtilities.toFix(D * Y, 10)); if ((Math.abs(M) > 100000) || (Math.abs(Y) > 100000) || (Math.abs(Y) == 1)) { this.numerator = original; this.denominator = BigInteger.ONE; } else { this.numerator = BigDecimal.valueOf(Math.round((Math.abs(M) / M) * (Math.abs(Y) / Y) * Math.abs(M))); this.denominator = new BigInteger(Long.toString(Math.round(Math.abs(Y)))); } }
@Override public String toString() { if (this.denominator.equals(BigInteger.ONE)) return MathUtilities.formatNormalAnswer(this.numerator); else return this.numerator + "/" + this.denominator; }