public int CompareTo(Rational other) { var sign = Sign.CompareTo(other.Sign); if (sign != 0) { return(sign); } if ((IsNaN && other.IsNaN) || (IsInfinity && other.IsInfinity)) { return(0); } if (IsNaN || other.IsNaN) { return(Denominator.CompareTo(other.Denominator)); } if (IsPositiveInfinity || other.IsNegativeInfinity) { return(1); } if (IsNegativeInfinity || other.IsPositiveInfinity) { return(-1); } var diff = this - other; return(diff.Sign); }
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 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)); }
private void Normalize() { // compute greatest common divisor of numerator and denominator BigInteger g = Numerator.Gcd(Denominator); if (g.CompareTo(BigInteger.One) > 0) { Numerator = Numerator.Divide(g); Denominator = Denominator.Divide(g); } if (Denominator.CompareTo(BigInteger.Zero) == -1) { Numerator = Numerator.Negate(); Denominator = Denominator.Negate(); } }
public override string ToString() { return(Denominator.CompareTo(BigInteger.One) != 0 ? Numerator + "/" + Denominator : Numerator.ToString()); }
public BigInteger Truncate() { return(Denominator.CompareTo(BigInteger.One) == 0 ? Numerator : Numerator.Divide(Denominator)); }