// bin search based square root computation, // works for rationals in the range [0,2147483647] public Rational root() throws IllegalArgumentToSquareRootException { Rational low = new Rational(0, 1); // Max Int value is 2147483647, square root is 46341 // Rational high = new Rational(46341,1); Rational high = new Rational(46340, 1); if (this.isLessThan(low) || !this.isLessThan(high)) { throw new IllegalArgumentToSquareRootException(this); } System.out.println("46341* 46341 = " + (46341 * 46341)); System.out.println("46340* 46340 = " + (46340 * 46340)); Rational half = new Rational(1, 2); Rational midpoint = (low.plus(high)).times(half); Rational tmp = null; while (!high.minus(low).abs().isLessThan(TOLERANCE)) { System.out.println("low high: " + low + " " + high); System.out.println("square of midpoint is: " + midpoint.times(midpoint)); if (midpoint.times(midpoint).isLessThan(this)) { tmp = new Rational(midpoint); midpoint = high.plus(midpoint).times(half); low = tmp; } else { tmp = new Rational(midpoint); midpoint = low.plus(midpoint).times(half); high = tmp; } } return midpoint; }
/** * Return the next downbeat (beat 1) after the given point * * @param r * @return */ public Rational nextDownBeat(Rational r) { Rational meterEstablishedAt = _data.floorKey(r); TimeSignature ts = _data.get(meterEstablishedAt); Rational result = meterEstablishedAt; Rational inc = Rational.get(ts.TOP); while (result.compareTo(r) <= 0) { result = result.plus(inc); } // Look ahead - if a new Time Signature overrode our // old time signature before it completed a measure // we need to compensate for that. Rational higher = _data.higherKey(r); if (higher != null && result.compareTo(higher) > 0) result = higher; return result; }
public void add() { operand_0 = operand_1.plus(operand_0); operand_1 = new Rational(); }
// to subtract, just add a negated version public Rational minus(Rational b) { Rational a = this; Rational c = new Rational(b._numerator * -1, b._denominator); return a.plus(c); }
/** Sommatie van operand 1 en operand 2 */ public void add() { operand_0 = operand_1.plus(operand_0); operand_1 = new Rational(); processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null)); }