Exemple #1
0
        /// <summary>
        /// Demo of this program. Only for developers seeing this code.
        /// </summary>
        static void Demo()
        {
            // if Euler's number e=2.71828... can be approximated by fractions like X1/2, X2/3, ... , X98/99,
            // where X1, ... ,X98 are integers, which one will be closest to e?
            // Let's check it.
            var fs = DecimalToFraction.ConvertDecimalToFraction(Math.E, 2, 99);

            Console.WriteLine("*** Euler's number e = {0}", Math.E);
            Console.WriteLine("[In order of denominator]");
            Console.WriteLine("---------------------------------------------------");
            foreach (var f in fs)
            {
                Console.WriteLine(f.ToString());
            }
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("[The closest fraction]");
            Console.WriteLine(DecimalToFraction.GetClosestFraction(fs).ToString());
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("[In order of the absolute value of error]");
            Console.WriteLine("---------------------------------------------------");
            foreach (var f in fs.OrderBy(f => Math.Abs(f.Error)))
            {
                Console.WriteLine(f.ToString());
            }
        }
Exemple #2
0
            /// <summary>
            /// Get the closest fraction to a given decimal number whose denominator is in the given range
            /// </summary>
            /// <param name="decim">a decimal number</param>
            /// <param name="denomRangeMin">the min of the range</param>
            /// <param name="denomRangeMax">the max of the range</param>
            /// <returns>The closest fraction to a given decimal number</returns>
            static public DecimalToFraction GetClosestFraction(double decim, int denomRangeMin, int denomRangeMax)
            {
                var fs = DecimalToFraction.ConvertDecimalToFraction(decim, denomRangeMin, denomRangeMax);

                return(DecimalToFraction.GetClosestFraction(fs));
            }
Exemple #3
0
        static void Main(string[] args)
        {
            //foreach (var a in args) Console.WriteLine("{0}", a);


            double d   = 0;
            int    min = 0;
            int    max = 0;

            if (!AreArgsValid(args))
            {
                Console.WriteLine("The parameters are invalid.");
                Console.WriteLine("");


                while (true)
                {
                    Console.WriteLine("Input a decimal number and minimum and maximum denominators");
                    Console.WriteLine("For example, if you want to approximate 3.1415 with denominators from 2 to 99, input '3.1415 2 99'");
                    Console.WriteLine("If you want to quit, just type 'q'");
                    string s = Console.ReadLine();

                    if (s == "q")
                    {
                        return;
                    }

                    args = s.Split(' ');
                    if (AreArgsValid(args))
                    {
                        break;
                    }

                    Console.WriteLine("The parameters are invalid.");
                    Console.WriteLine("");
                }
            }

            d   = double.Parse(args[0]);
            min = int.Parse(args[1]);
            max = int.Parse(args[2]);

            var fs = DecimalToFraction.ConvertDecimalToFraction(d, min, max);

            Console.WriteLine("[approximated fractions of {0}]", d);
            Console.WriteLine("(In order of denominator)");
            Console.WriteLine("---------------------------------------------------");

            foreach (var f in fs)
            {
                Console.WriteLine($"{f.Numerator}/{f.Denominator}\t(error : {(f.Error*100).ToString("G3")}%)");
            }
            Console.WriteLine("");
            Console.WriteLine("[The closest fraction]");
            var closest = DecimalToFraction.GetClosestFraction(fs);

            if (closest != null)
            {
                Console.WriteLine($"{closest.Numerator}/{closest.Denominator}\t(error : {(closest.Error * 100).ToString("G3")}%)");
            }
            else
            {
                Console.WriteLine("none");
            }
            Console.WriteLine("");

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
            return;
        }