Beispiel #1
0
 public Rational AddRational(Rational first)
 {
     Rational newRational;
     newRational._numerator = (first._numerator*this._denominator) + (this._numerator*first._denominator);
     newRational._denominator = first._denominator*this._denominator;
     return newRational;
 }
Beispiel #2
0
 public Rational SubtractRational(Rational first)
 {
     Rational newRational;
     newRational._numerator = (first._numerator * this._denominator) - (this._numerator * first._denominator);
     newRational._denominator = first._denominator * this._denominator;
     return newRational;
 }
Beispiel #3
0
 public Rational DevideRational(Rational first)
 {
     Rational newRational;
     newRational._numerator = (first.ReduceRational()._numerator * this.ReduceRational()._denominator);
     newRational._denominator = first.ReduceRational()._denominator * this.ReduceRational()._numerator;
     return newRational;
 }
Beispiel #4
0
        static void Main(string[] args)
        {
            Rational first = new Rational();
            Rational second = new Rational();
            first.Numerator = 2;
            first.Denominator = 4;
            second.Numerator = 4;
            second.Denominator = 8;

            Console.WriteLine("the numbers are:");
            Console.WriteLine(first.ToString());
            Console.WriteLine(second.ToString());
        
            Console.WriteLine();
            Console.WriteLine("the decimal value is:");
            Console.WriteLine(first.Decimalvalue);
            Console.WriteLine(second.Decimalvalue);
          
            Console.WriteLine();
            Console.WriteLine("the reduced rationals are:");
            Console.WriteLine(first.ReduceRational().ToString());
            Console.WriteLine(second.ReduceRational().ToString());

            Console.WriteLine();
            Console.WriteLine("the added rationals are:");
            Console.WriteLine(first.AddRational(second).ToString());

            Console.WriteLine();
            Console.WriteLine("the Multiply of the rationals are:");
            Console.WriteLine(first.MulRational(second).ToString());

            Console.WriteLine();
            Console.WriteLine("are the two rational equal");
            Console.WriteLine(first.Equals(second));

            Console.WriteLine();
            Console.WriteLine("addition");
            Rational addition = first + second;
            Console.WriteLine(addition.ToString());

            Console.WriteLine();
            Console.WriteLine("subtruct");
            Rational subtruct = first - second;
            Console.WriteLine(subtruct.ToString());

            Console.WriteLine();
            Console.WriteLine("devide");
            Rational devide = first / second;
            Console.WriteLine(devide.ToString());

            Console.WriteLine();
            Console.WriteLine("multiply");
            Rational multiply = first * second;
            Console.WriteLine(multiply.ToString());
            
            Console.ReadLine();
        }
Beispiel #5
0
        public static fp_t Sum(this IEnumerable <fp_t> data)
        {
            fp_t sum = 0;

            foreach (fp_t t in data)
            {
                sum += t;
            }
            return(sum);
        }
Beispiel #6
0
 /// <inheritdoc cref="ParseDecimal(string,decimal)"/>
 public static bool TryParseDecimal(string value, NumberStyles style, IFormatProvider provider, out Rational result, decimal tolerance = 0)
 {
     return(TryParseDecimal(value, style, NumberFormatInfo.GetInstance(provider), out result, tolerance));
 }
Beispiel #7
0
 public Rational Mul(Rational other)
 {
     return new Rational(_numerator * other._numerator, _denumerator * other._denumerator);
 }
Beispiel #8
0
 public string FormatRational(Rational r)
 {
     return(FormatCents(r.ToCents()));
 }
Beispiel #9
0
 public bool Equals(Rational other)
 {
     return _denominator == other._denominator && _numerator == other._numerator;
 }
Beispiel #10
0
        private static void InnerCustomOperation(Func <roll_t[], roll_t> f, List <roll_t> before, DiceRoll[] dice, fp_t existing_p, DiceRoll d)
        {
            DiceRoll r = dice[before.Count];

            foreach (var m in r.rolls)
            {
                var a = before.ToList();
                a.Add(m.Key);

                if (before.Count + 1 == dice.Length)
                {
                    // innermost dice, modify d
                    var n = f(a.ToArray());
                    if (!d.rolls.ContainsKey(n))
                    {
                        d.rolls[n] = 0;
                    }
                    d.rolls[n] += existing_p * m.Value;
                }
                else
                {
                    // we need to go deeper!
                    InnerCustomOperation(f, a, dice, existing_p * m.Value, d);
                }
            }
        }
Beispiel #11
0
        private static bool TryParse(string value, NumberStyles style, NumberFormatInfo info, out Rational result)
        {
            result = default;

            try
            {
                var match = FractionFormat.Match(value);
                if (match.Success)
                {
                    var numerator   = BigInteger.Parse(match.Groups["Numerator"].Value, style, info);
                    var denominator = BigInteger.Parse(match.Groups["Denominator"].Value, style, info);
                    result = new Rational(numerator, denominator);
                    return(true);
                }

                match = WholeFractionalFormat.Match(value);
                if (match.Success)
                {
                    var whole       = BigInteger.Parse(match.Groups["Whole"].Value, style, info);
                    var numerator   = BigInteger.Parse(match.Groups["Numerator"].Value, style, info);
                    var denominator = BigInteger.Parse(match.Groups["Denominator"].Value, style, info);
                    result = new Rational(whole) + new Rational(numerator, denominator);
                    return(true);
                }

                match = NaNFormat.Match(value);
                if (match.Success)
                {
                    result = NaN;
                    return(true);
                }
            }
            catch (DivideByZeroException)
            {
                return(false);
            }

            if (BigInteger.TryParse(value, style, info, out var justWhole))
            {
                result = new Rational(justWhole);
                return(true);
            }

            return(false);
        }
Beispiel #12
0
 public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, out Rational result)
 {
     return(TryParse(value, style, NumberFormatInfo.GetInstance(provider), out result));
 }
Beispiel #13
0
 public static bool TryParse(string value, out Rational result)
 {
     return(TryParse(value, NumberStyles.Float, NumberFormatInfo.CurrentInfo, out result));
 }
Beispiel #14
0
 public Rational Add(Rational other)
 {
     return new Rational(_numerator * other._denumerator + other._numerator * _denumerator, _denumerator * other._denumerator);
 }
 public Rational Mul(Rational rat)
 {
     int newNumerator = _numerator * rat.Numerator;
     int newDenominator = _denominator * rat.Denominator;
     return new Rational(newNumerator, newDenominator);
 }
Beispiel #16
0
 public static bool TryParseDecimal(string value, out Rational result, decimal tolerance = 0)
 {
     return(TryParseDecimal(value, NumberStyles.Float, NumberFormatInfo.CurrentInfo, out result, tolerance));
 }
Beispiel #17
0
        public static Rational MakeNarrow(int narrowIndex, int basePrimeIndex)
        {
            Rational r = Rational.Prime(narrowIndex); // just prime: 2/1, 3/1, 5/1, 7/1, ..

            return(MakeNarrow(r, basePrimeIndex));
        }
Beispiel #18
0
 public string FormatNarrowPowers(Rational r)
 {
     return(r.FormatNarrowPowers(_narrows));
 }
Beispiel #19
0
 public Pow[] GetNarrowPowers(Rational r)
 {
     return(r.GetNarrowPowers(_narrows));
 }
Beispiel #20
0
 public bool IsInRange(Rational r)
 {
     return(_matrix != null && _matrix.FindCoordinates(r) != null);
 }
 public Rational Add(Rational rat)
 {
     int newNumerator = (_numerator * rat.Denominator) + (_denominator * rat.Numerator);
     int newDenominator = _denominator * rat.Denominator;
     return new Rational(newNumerator, newDenominator);
 }
Beispiel #22
0
        private static bool TryParseDecimal(string value, NumberStyles style, NumberFormatInfo info, out Rational result, decimal tolerance = 0)
        {
            if (!decimal.TryParse(value, style, info, out var d))
            {
                result = default(Rational);
                return(false);
            }

            try
            {
                result = Approximate(d, tolerance);
                return(true);
            }
            catch (Exception)
            {
                result = default(Rational);
                return(false);
            }
        }