コード例 #1
0
ファイル: Program.cs プロジェクト: inktan/Towel
        static void Main()
        {
            Console.WriteLine("You are runnning the Mathematics example.");
            Console.WriteLine("==========================================");
            Console.WriteLine();

            Random random = new Random();

            #region Basic Operations

            Console.WriteLine("  Basics----------------------------------------------");
            Console.WriteLine();

            // Variables
            Fraction <short>          clampA       = new Fraction <short>(-123, 9); // -123 / 9
            Fraction <short>          clampB       = new Fraction <short>(7, 12);   //    7 / 12
            Fraction <short>          clampC       = new Fraction <short>(14, 15);  //   14 / 15
            double[]                  values       = new double[4];
            Action <Action <double> > valueStepper = values.ToStepper();
            values.Format(x => random.NextDouble());

            // Examples

            double negation = Negation(7);
            Console.WriteLine("    Negate(7): " + negation);

            decimal addition = Addition(7m, 7m);
            Console.WriteLine("    Add(7, 7): " + addition);

            double summation = Σ(valueStepper);
            Console.WriteLine("    Σ (" + string.Join(", ", values.Select(x => Format(x))) + ") = " + summation);

            float subtraction = Subtraction(14f, 7f);
            Console.WriteLine("    Subtract(14, 7): " + subtraction);

            long multiplication = Multiplication(7L, 7L);
            Console.WriteLine("    Multiply(7, 7): " + multiplication);

            double product = Π(valueStepper);
            Console.WriteLine("    Π (" + string.Join(", ", values.Select(x => Format(x))) + ") = " + product);

            short division = Division((short)14, (short)7);
            Console.WriteLine("    Divide(14, 7): " + division);

            double absoluteValue = AbsoluteValue(-7d);
            Console.WriteLine("    AbsoluteValue(-7): " + absoluteValue);

            Fraction <short> clamp = Clamp(clampA, clampB, clampC);
            Console.WriteLine("    Clamp(" + clampA + ", " + clampB + ", " + clampC + "): " + clamp);

            int maximum = MaximumValue <int>(compare: null, 1, 2, 3);
            Console.WriteLine("    MaximumValue(compare: null, 1, 2, 3): " + maximum);

            int minimum = Minimum(1, 2, 3);
            Console.WriteLine("    Minimum(1, 2, 3): " + minimum);

            bool lessThan = LessThan((Fraction <int>) 1, (Fraction <int>) 2);
            Console.WriteLine("    LessThan(1, 2): " + lessThan);

            bool greaterThan = GreaterThan((Fraction <int>) 1, (Fraction <int>) 2);
            Console.WriteLine("    GreaterThan(1, 2): " + greaterThan);

            CompareResult compare = Compare((Fraction <short>) 7, (Fraction <short>) 7);
            Console.WriteLine("    Compare(7, 7): " + compare);

            bool equality = Equate(7, 6);
            Console.WriteLine("    Equate(7, 6): " + equality);

            bool equalsLeniency = EqualToLeniency(2, 1, 1);
            Console.WriteLine("    EqualsLeniency(2, 1, 1): " + equalsLeniency);

            Console.WriteLine();

            #endregion

            #region More Numeric Mathematics

            Console.WriteLine("  More Numeric Mathematics----------------------------");
            Console.WriteLine();

            // some random ints for the examples
            int   random1     = random.Next(1, 100000);
            int   random2     = random.Next(1, 1000);
            int   random3     = random.Next(1, 1000);
            int   random4     = random.Next(1, 1000);
            int   random5     = random.Next(1, 1000);
            int   random6     = random.Next(1, 1000);
            int   random7     = random.Next(6, 10);
            int   random8     = random.Next(1, 100000);
            int[] randomInts1 = new int[3];
            randomInts1.Format(x => random.Next(1, 500) * 2);
            Action <Action <int> > randomInts1Stepper = randomInts1.ToStepper();
            int[] randomInts2 = new int[3];
            randomInts2.Format(x => random.Next(1, 500) * 2);
            Action <Action <int> > randomInts2Stepper = randomInts2.ToStepper();

            bool isPrime = IsPrime(random1);
            Console.WriteLine("    IsPrime(" + random1 + "): " + isPrime);

            bool isNegative = IsNegative(random2);
            Console.WriteLine("    IsNegative(" + random2 + "): " + isNegative);

            bool isNonNegative = IsNonNegative(random3);
            Console.WriteLine("    IsNonNegative(" + random3 + "): " + isNonNegative);

            bool isPositive = IsPositive(random4);
            Console.WriteLine("    IsPositive(" + random4 + "): " + isPositive);

            bool isOdd = IsOdd(random5);
            Console.WriteLine("    IsOdd(" + random5 + "): " + isOdd);

            bool isEven = IsEven(random6);
            Console.WriteLine("    IsEven(" + random6 + "): " + isEven);

            int greatestCommonFactor = GreatestCommonFactor(randomInts1Stepper);
            Console.WriteLine("    GCF(" + string.Join(", ", randomInts1) + "): " + greatestCommonFactor);

            int leastCommonMultiple = LeastCommonMultiple(randomInts2Stepper);
            Console.WriteLine("    LCM(" + string.Join(", ", randomInts2) + "): " + leastCommonMultiple);

            int factorial = Factorial(random7);
            Console.WriteLine("    " + random7 + "!: " + factorial);

            int combinations = Combinations(7, Ɐ(3, 4));
            Console.WriteLine("    7! / (3! * 4!): " + combinations);

            int binomialCoefficient = BinomialCoefficient(7, 2);
            Console.WriteLine("    7 choose 2: " + binomialCoefficient);

            Console.Write("    Prime Factors(" + random8 + "): ");
            FactorPrimes(random8, prime => Console.Write(prime + " "));
            Console.WriteLine();

            Console.WriteLine();

            #endregion

            #region Trigonometry

            Console.WriteLine("  Trigonometry -----------------------------------------");
            Console.WriteLine();

            double         randomDouble = random.NextDouble();
            Angle <double> randomAngle  = new Angle <double>(randomDouble, Angle.Units.Radians);

            double sineTaylorSeries = SineTaylorSeries(randomAngle);
            Console.WriteLine("    SinTaylorSeries(" + randomAngle + ") = " + Format(sineTaylorSeries));

            double cosineTaylorSeries = CosineTaylorSeries(randomAngle);
            Console.WriteLine("    CosinTaylorSeries(" + randomAngle + ") = " + Format(cosineTaylorSeries));

            Console.WriteLine();

            #endregion

            #region Statistics

            Console.WriteLine("  Statistics-----------------------------------------");
            Console.WriteLine();

            // Data Generation
            double   mode_temp = random.NextDouble() * 100;
            double[] dataArray = new double[random.Next(5, 7)];
            dataArray.Format(x => random.NextDouble() * 100);

            // Lets copy a value in the array to ensure there is at least one
            // duplicate (so the "Mode" example will has something to show)
            dataArray[^ 1] = dataArray[0];
コード例 #2
0
 /// <summary>
 /// Compara duas fracções.
 /// </summary>
 /// <param name="x">A primeira fracção a ser comparada.</param>
 /// <param name="y">A segunda fracção a ser comparada.</param>
 /// <returns>
 /// O valor 1 caso a primeira seja superior à segunda, 0 caso sejam iguais e -1 caso contrário.
 /// </returns>
 public int Compare(Fraction <CoeffType> x, Fraction <CoeffType> y)
 {
     return(this.fractionComparer.Compare(x, y));
 }