コード例 #1
0
        public static void Div(Real a, Real b)
        {
            Console.WriteLine();
            Console.WriteLine("DIVISION");
            Console.WriteLine("--------");
            Console.WriteLine("First number =  " + a.ToFloat());
            Console.WriteLine("Second number = " + b.ToFloat());
            Console.WriteLine();
            Console.WriteLine("In two's complement:");
            Console.WriteLine("First number =  " + a.ToFormat());
            Console.WriteLine("Second number = " + b.ToFormat());
            Console.WriteLine();

            long SignA = (a.Sign == 1 ? -1 : 1), SignB = (b.Sign == 1 ? -1 : 1);
            long ExpA = a.Exp - Real.ExpSecret, ExpB = b.Exp - Real.ExpSecret;
            long MantA = a.Mant + Real.MantSecret, MantB = b.Mant + Real.MantSecret;

            if (b.EqualsZero())
            {
                Console.WriteLine("ZERO DIVISION!");
                return;
            }

            if (a.EqualsZero())
            {
                Real s = new Real(0.0f);
                Console.WriteLine("First number equals zero:");
                Console.WriteLine("Product = " + s.ToFormat());
                Console.WriteLine("Product = " + s.ToFloat());
                return;
            }

            long SignQ = SignA != SignB ? -1 : 1;
            long ExpQ  = ExpA - ExpB;
            long MantQ = (MantA * Real.MantSecret).RoundedDivision(MantB);

            while (MantQ >= (Real.MantSecret << 1))
            {
                MantQ >>= 1;
                ExpQ++;
            }
            while (MantQ < (1 << 23))
            {
                MantQ <<= 1;
                ExpQ--;
            }
            if (ExpQ <= -Real.ExpSecret)
            {
                Console.WriteLine("UNDERFLOW!");
                return;
            }
            if (Real.ExpSecret < ExpQ)
            {
                Console.WriteLine("OVERFLOW!");
                return;
            }

            Real Q = new Real(SignQ, ExpQ, MantQ);

            Console.WriteLine("Quotient = " + Q.ToFormat());
            Console.WriteLine("Quotient = " + Q.ToFloat());
        }
コード例 #2
0
        public static void Add(Real a, Real b)
        {
            Console.WriteLine();
            Console.WriteLine("ADDITION");
            Console.WriteLine("--------");
            Console.WriteLine("First number =  " + a.ToFloat());
            Console.WriteLine("Second number = " + b.ToFloat());
            Console.WriteLine();
            Console.WriteLine("In two's complement:");
            Console.WriteLine("First number =  " + a.ToFormat());
            Console.WriteLine("Second number = " + b.ToFormat());
            Console.WriteLine();

            long SignA = (a.Sign == 1 ? -1 : 1), SignB = (b.Sign == 1 ? -1 : 1);
            long ExpA = a.Exp - Real.ExpSecret, ExpB = b.Exp - Real.ExpSecret;
            long MantA = a.Mant + Real.MantSecret, MantB = b.Mant + Real.MantSecret;

            if (a.EqualsZero() || b.EqualsZero())
            {
                Real s = a.EqualsZero() ? b : a;
                Console.WriteLine("One of the numbers equals zero:");
                Console.WriteLine("Summ = " + s.ToFormat());
                Console.WriteLine("Summ = " + s.ToFloat());
                return;
            }

            while (ExpA < ExpB)
            {
                ExpA++;
                MantA >>= 1;
            }
            while (ExpB < ExpA)
            {
                ExpB++;
                MantB >>= 1;
            }
            Console.WriteLine("After normalization:");
            Console.WriteLine("First number =  " + a.ToFormat());
            Console.WriteLine("Second number = " + b.ToFormat());
            Console.WriteLine();
            if (MantA == 0 || MantB == 0)
            {
                Real s = MantA == 0 ? b : a;
                Console.WriteLine("One of the numbers is almost zero:");
                Console.WriteLine("Summ = " + s.ToFormat());
                Console.WriteLine("Summ = " + s.ToFloat());
                return;
            }

            long ExpS  = ExpA;
            long MantS = MantA * SignA + MantB * SignB;
            long SignS = (MantS < 0 ? -1 : 1);

            MantS *= SignS;

            if (MantS == 0)
            {
                Real s = new Real(0.0f);
                Console.WriteLine("Summ of the numbers is zero:");
                Console.WriteLine("Summ = " + s.ToFormat());
                Console.WriteLine("Summ = " + s.ToFloat());
                return;
            }

            while (MantS >= (Real.MantSecret << 1))
            {
                MantS >>= 1;
                ExpS++;
            }
            while (MantS < (1 << 23))
            {
                MantS <<= 1;
                ExpS--;
            }
            if (ExpS <= -Real.ExpSecret)
            {
                Console.WriteLine("UNDERFLOW!");
                return;
            }
            if (Real.ExpSecret < ExpS)
            {
                Console.WriteLine("OVERFLOW!");
                return;
            }

            Real S = new Real(SignS, ExpS, MantS);

            Console.WriteLine("Summ = " + S.ToFormat());
            Console.WriteLine("Summ = " + S.ToFloat());
        }