Example #1
0
        public static Floatz Multiply(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();

            if (a.isZero() || b.isZero())
            {
                return(Floatz.Constants.ZERO(a.separator));
            }

            List <int> fullA = a.ToOneList();
            List <int> fullB = b.ToOneList();
            List <int> fullC;

            if (a.zecimala.Count >= b.zecimala.Count)
            {
                fullC = Multiply2Lists(fullA, fullB);
            }
            else
            {
                fullC = Multiply2Lists(fullB, fullA);
            }

            int sepPlace = 0;

            if (!a.isZecimalaZero())
            {
                sepPlace += a.zecimala.Count;
            }
            if (!b.isZecimalaZero())
            {
                sepPlace += b.zecimala.Count;
            }

            Floatz c = new Floatz(a.separator);

            c            = Floatz.FromOneList(fullC, sepPlace);
            c.isNegative = (a.isNegative || b.isNegative);
            if (a.isNegative && b.isNegative)
            {
                c.isNegative = false; //-1*-1=1
            }
            c.ComprimaIntreaga();
            c.ComprimaZecimala();

            return(c);
        }
Example #2
0
        public static Floatz Divide(Floatz refA, Floatz refB, long nrMaxZecimalaLaImpartire = 74)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();

            if (a.isZero())
            {
                return(new Floatz(a.separator));
            }
            if (b.isZero())
            {
                throw new Exception("a/0");
            }
            if (a == b)
            {
                return(Floatz.Constants.ONE(a.separator));
            }

            List <int> fullB = b.ToOneList();

            if (!b.isZecimalaZero())
            {
                string mult = "1";
                for (int i = 0; i < b.zecimala.Count; i++)
                {
                    mult += "0";
                }

                a = a * new Floatz(mult);
            }

            //a.Print();
            //Console.WriteLine(Floatz.ListToString(fullB));
            Floatz c = new Floatz();

            c = DivideFloatzWithList(a, fullB, nrMaxZecimalaLaImpartire);
            //c.Print();

            c.separator  = a.separator;
            c.isNegative = (a.isNegative || b.isNegative);
            if (a.isNegative && b.isNegative)
            {
                c.isNegative = false; //-1*-1=1
            }
            return(c);
        }
Example #3
0
        public static Floatz Subtract(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();
            Floatz c = new Floatz(a.separator);

            if (a.isZero() && b.isZero())
            {
                return(Floatz.Constants.ZERO(a.separator));
            }
            if (a.isZero())
            {
                c.intreaga   = b.intreaga;
                c.zecimala   = b.zecimala;
                c.isNegative = !b.isNegative;
                return(c);
            }
            if (b.isZero())
            {
                c.intreaga   = a.intreaga;
                c.zecimala   = a.zecimala;
                c.isNegative = a.isNegative;
                return(c);
            }
            //Console.WriteLine("{0}|{1}",Floatz.ListToString(a.intreaga), Floatz.ListToString(a.zecimala));

            /*
             * 5-3
             * if(5<3)
             *  return -(5-3)
             * else
             *  return 5-3
             */

            if (!a.isNegative && b.isNegative)
            {
                b.isNegative = false;
                c            = Add(a, b);
                return(c);
            }
            if (a.isNegative && !b.isNegative)
            {
                a.isNegative = false;
                c            = Add(a, b);
                c.isNegative = true;
                return(c);
            }
            if (a < b)
            {
                c            = Subtract(b, a);
                c.isNegative = true;
                return(c);
            }

            // Aceasta functie este construita in asa fel incat a > b
            List <int> fullA = a.ToOneList();

            if (a.isZecimalaZero())
            {
                fullA.Add(0);
            }

            List <int> fullB = b.ToOneList();

            if (b.isZecimalaZero())
            {
                fullB.Add(0);
            }

            for (int i = 0; i < a.intreaga.Count - b.intreaga.Count; i++)
            {
                fullB.Insert(0, 0);
            }
            for (int i = 0; i < b.intreaga.Count - a.intreaga.Count; i++)
            {
                fullA.Insert(0, 0);
            }
            for (int i = 0; i < a.zecimala.Count - b.zecimala.Count; i++)
            {
                fullB.Add(0);
            }
            for (int i = 0; i < b.zecimala.Count - a.zecimala.Count; i++)
            {
                fullA.Add(0);
            }


            List <int> fullC  = Subtract2Lists(fullA, fullB);
            int        fullCi = 0;

            fullCi = Math.Max(a.zecimala.Count, b.zecimala.Count);

            c = Floatz.FromOneList(fullC, fullCi);
            c.ComprimaIntreaga();
            c.ComprimaZecimala();
            return(c);
        }