public int CompareTo(Fraction other) { if (Numerator > other.Numerator && Denominator < other.Denominator) { return(1); } if (Numerator < other.Numerator && Denominator > other.Denominator) { return(-1); } if (Numerator == other.Numerator) { return(other.Denominator.CompareTo(Denominator)); } if (Denominator == other.Denominator) { return(Numerator.CompareTo(other.Numerator)); } if (Equals(other)) { return(0); } var lcd = Lcd(Denominator, other.Denominator); var multiplierThis = lcd / Denominator; var multiplierOther = lcd / other.Denominator; return((Numerator * multiplierThis).CompareTo(other.Numerator * multiplierOther)); }
public BigInteger Ceiling() { if (Denominator.CompareTo(BigInteger.One) == 0) { return(Numerator); } if (Numerator.CompareTo(BigInteger.Zero) > 0) { return(Numerator.Divide(Denominator).Add(BigInteger.One)); } return(Numerator.Divide(Denominator)); }
public int CompareTo(object obj) { Fraction temp = (Fraction)obj; if (Denominator == temp.Denominator) { return(Numerator.CompareTo(temp.Numerator)); } else { return((Numerator * temp.Denominator).CompareTo(temp.Numerator * Denominator)); } }
public BigInteger Floor() { /* is already integer: return the numerator */ if (Denominator.CompareTo(BigInteger.One) == 0) { return(Numerator); } if (Numerator.CompareTo(BigInteger.Zero) > 0) { return(Numerator.Divide(Denominator)); } return(Numerator.Divide(Denominator).Subtract(BigInteger.One)); }
public int CompareTo(Fraction that) { if (ReferenceEquals(this, that)) { return(0); } if (that is null) { return(1); } var denominatorComparison = Denominator.CompareTo(that.Denominator); if (denominatorComparison != 0) { return(denominatorComparison); } return(Numerator.CompareTo(that.Numerator)); }
public infRational(string input1, string input2) { //explicit value constructor 2 this.Numerator = new InfInt(input1); this.Denominator = new InfInt(input2); if ((Numerator.CompareTo(new InfInt("0")) < 0) || Denominator.CompareTo(new InfInt("0")) < 0) // if either numerator or denominator is negative { if ((Numerator.CompareTo(new InfInt("0")) < 0) && Denominator.CompareTo(new InfInt("0")) < 0) // if both are negative { Positive = true; } else { Positive = false; } } else { Positive = true; } }
public infRational(InfInt num, InfInt denom) { //explicit value constructor 1 this.Numerator = num; this.Denominator = denom; if ((Numerator.CompareTo(new InfInt("0")) < 0) || Denominator.CompareTo(new InfInt("0")) < 0) // if either numerator or denominator is negative { if ((Numerator.CompareTo(new InfInt("0")) < 0) && Denominator.CompareTo(new InfInt("0")) < 0) // if both are negative { Positive = true; } else { Positive = false; } } else { Positive = true; } }
public int CompareTo(Fraction other) { if (Denominator > 0 && Denominator == other.Denominator) { return(Numerator.CompareTo(other.Numerator)); } if (Denominator < 0 && Denominator == other.Denominator) { return(-Numerator.CompareTo(other.Numerator)); } long leftDenominatorSign = Denominator < 0 ? -1 : 1; long rightDenominatorSign = other.Denominator < 0 ? -1 : 1; long sign = leftDenominatorSign * rightDenominatorSign; long leftNumerator = sign * Numerator * other.Denominator; long rightNumerator = sign * other.Numerator * Denominator; return(leftNumerator.CompareTo(rightNumerator)); }