Ejemplo n.º 1
0
        public static Rational FromString(string rational)
        {
            Rational result;
            if (Regex.Match(rational, @"^[+-]?\d+\/\d+").Value == rational)
            {
                var numerator = int.Parse(rational.Substring(0, rational.LastIndexOf('/')));
                var denominator = int.Parse(rational.Substring(rational.LastIndexOf('/') + 1));
                result = Simplify(numerator, denominator);
            }
            else if (Regex.Match(rational, @"^[+-]?\d+(\.\d+)?").Value == rational)
            {
                if (rational.LastIndexOf('.') == -1)
                {
                    result = new Rational(int.Parse(rational));
                }
                else
                {
                    result = FromDouble(double.Parse(rational));
                }
            }
            else
            {
                throw new RationalException("Cadena invalida", new FormatException(rational + " not valid."));
            }

            return result;
        }
Ejemplo n.º 2
0
        public static bool Eq(this Rational a, Rational b)
        {
            bool areEquals = !AreNullOperands(a, b) &&
                (a.Sub(b).Numerator == 0 || Rational.ReferenceEquals(a, b));

            return areEquals;
        }
Ejemplo n.º 3
0
        public static Rational Add(this Rational a, Rational b)
        {
            AssertNotNullOperand(a);
            AssertNotNullOperand(b);

            var resultNumerator = (a.Sign * a.Numerator * b.Denominator) + (b.Sign * b.Numerator * a.Denominator);
            var resultDenominator = a.Denominator * b.Denominator;

            return new Rational(resultNumerator, resultDenominator);
        }
Ejemplo n.º 4
0
        public static Rational Mul(this Rational a, Rational b)
        {
            AssertNotNullOperand(a);
            AssertNotNullOperand(b);

            var resultNumerator = a.Numerator * b.Numerator;
            var resultDenominator = a.Denominator * b.Denominator;
            var resultSign = a.Sign * b.Sign;

            return new Rational(resultNumerator, resultDenominator, resultSign);
        }
Ejemplo n.º 5
0
        public static string ToString(Rational rational)
        {
            var displayValue = "0";
            if (rational.Numerator != 0)
            {
                displayValue = (rational.Sign != 1 ? "-" : string.Empty)
                + rational.Numerator
                + (rational.Denominator != 1 ? "/" + rational.Denominator : string.Empty);
            }

            return displayValue;
        }
Ejemplo n.º 6
0
        public static bool Gt(this Rational a, Rational b)
        {
            bool isGreaterThan = false;

            if (!AreNullOperands(a, b))
            {
                var diff = a.Sub(b);
                isGreaterThan = diff.Numerator != 0 && diff.Sign > 0;
            }

            return isGreaterThan;
        }
Ejemplo n.º 7
0
        public static Rational FromDouble(double rational)
        {
            Rational result;
            if (rational - Math.Truncate(rational) != 0)
            {
                var pow = 1;
                var decimalCount = rational.ToString().Substring(rational.ToString().LastIndexOf('.') + 1).Length;
                for (int i = 0; i < decimalCount; i++)
                {
                    pow *= 10;
                }

                result = Simplify((int)(rational * pow), pow);
            }
            else
            {
                result = new Rational((int)rational);
            }

            return result;
        }
Ejemplo n.º 8
0
 private static bool AreNullOperands(this Rational a, Rational b)
 {
     return ((object)a == null) || ((object)b == null);
 }
Ejemplo n.º 9
0
        public static Rational Sub(this Rational a, Rational b)
        {
            var inverseAditive = b.Mul(new Rational(-1));

            return a.Add(inverseAditive);
        }
Ejemplo n.º 10
0
 public static bool Ne(this Rational a, Rational b)
 {
     return !a.Eq(b);
 }
Ejemplo n.º 11
0
 public static bool Lt(this Rational a, Rational b)
 {
     return b.Gt(a);
 }
Ejemplo n.º 12
0
 public static bool Le(this Rational a, Rational b)
 {
     return b.Ge(a);
 }
Ejemplo n.º 13
0
 public static bool Ge(this Rational a, Rational b)
 {
     return a.Gt(b) || a.Eq(b);
 }
Ejemplo n.º 14
0
        public static Rational Div(this Rational a, Rational b)
        {
            var inverseMultiplicative = new Rational(b.Denominator, b.Numerator, b.Sign);

            return a.Mul(inverseMultiplicative);
        }