コード例 #1
0
        public static void Run()
        {
            const int limit = 10000;

            Console.WriteLine($"ADDING {limit:N0} RANDOM ONE'S AND TWO'S ...");
            Console.WriteLine("(2 overly-complex, bad approaches to a simple problem, just to demonstrate the pattern)");
            Console.WriteLine();
            Console.WriteLine("1st approach: create a new object for each random 1 or 2 that is added");
            Console.WriteLine();

            var rnd    = new Random();
            var ones   = new List <Addend>();
            var twos   = new List <Addend>();
            var result = 0;

            for (var i = 0; i < limit; i++)
            {
                var next = new Addend(rnd);
                next.SetValue();
                result = next.Add(result);

                if (next.AddendValue == 1)
                {
                    ones.Add(next);
                }
                if (next.AddendValue == 2)
                {
                    twos.Add(next);
                }
                PrintProgress(result, i + 1, limit, ones.Count, twos.Count, Addend.InstanceCount);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("2nd approach: use flyweight pattern to reduce number of instances created");
            Console.WriteLine();
            var fwOnes        = new List <IFlyweightAddend>();
            var fwTwos        = new List <IFlyweightAddend>();
            var addendFactory = new FlyweightAddendFactory();

            result = 0;

            for (var i = 0; i < limit; i++)
            {
                var addendValue = rnd.Next(1, 3);
                var next        = addendFactory.GetAddend(addendValue);
                result = next.Add(result);

                if (addendValue == 1)
                {
                    fwOnes.Add(next);
                }
                if (addendValue == 2)
                {
                    fwTwos.Add(next);
                }
                PrintProgress(result, i + 1, limit, fwOnes.Count, fwTwos.Count, FlyweightAddend.InstanceCount);
            }
        }
コード例 #2
0
 public void NullReferenceExceptionBugTest()
 {
     var source = new Addend(1);
     var target = new NullReferenceExceptionBug();
     CalculatedProperty.Create(
         source.GetProperty(o => o.AddendValue), v => v, target.GetProperty(o => o.Value)).Dispose();
 }
コード例 #3
0
    /* Operators */
    public static MiniFloat operator +(MiniFloat Augend, MiniFloat Addend)
    {
        MiniFloat m = Augend.Copy();
        MiniFloat n = Addend.Copy();

        m.Mantissa = '1' + m.Mantissa;
        n.Mantissa = '1' + n.Mantissa;
        Normalize(ref m, ref n);

        if (m.Signbit == '0' && n.Signbit == '0')
        {
            return(add(m, n));
        }
        else if ((m.Signbit == '1' && n.Signbit == '0') || (n.Signbit == '1' && m.Signbit == '0'))
        {
            if (m.Mantissa > n.Mantissa)
            {
                return(sub(m, n));
            }
            else if (n.Mantissa > m.Mantissa)
            {
                return(sub(n, m));
            }
            else /* Equal */ return {
                (new MiniFloat());                   //Zero
            }
        }
        else     //Both are negative
        {
            MiniFloat r = add(m, n);
            r.Signbit = '1';
            return(r);
        }
    }
コード例 #4
0
        public void NullReferenceExceptionBugTest()
        {
            var source = new Addend(1);
            var target = new NullReferenceExceptionBug();

            CalculatedProperty.Create(
                source.GetProperty(o => o.AddendValue), v => v, target.GetProperty(o => o.Value)).Dispose();
        }
コード例 #5
0
ファイル: Binary.cs プロジェクト: matthew-e-brown/MiniFloats
    /* -- End of Operators for Concatenation -- */

    public static Binary operator +(Binary Augend, Binary Addend)
    {
        Binary m = Augend.Copy();
        Binary n = Addend.Copy();

        /* Normalize to the longest one */
        int l;

        if (m.Length > n.Length)
        {
            l = m.Length;
            for (int i = n.Length; i < m.Length; i++)
            {
                n = '0' + n;
            }
        }
        else if (m.Length < n.Length)
        {
            l = n.Length;
            for (int i = m.Length; i < n.Length; i++)
            {
                m = '0' + m;
            }
        }
        else
        {
            l = m.Length;
        }

        Binary c = new Binary(l);

        for (int i = l - 1; i >= 0; i--)
        {
            int x = MainClass.ValueOf(m[i]) + MainClass.ValueOf(n[i]) + MainClass.ValueOf(c[i]);
            c[i] = MainClass.Character(x);
            if (c[i] == '2' || c[i] == '3')
            {
                c[i] = (c[i] == '2' ? '0' : '1');
                if (i - 1 < 0)   //If it's the last one...
                {
                    c = '1' + c;
                }
                else
                {
                    c[i - 1] = '1';
                }
            }
        }
        return(c);
    }
コード例 #6
0
 public string GetAddendString()
 {
     return(Addend.ToString());
 }