コード例 #1
0
ファイル: FloatzOperators.cs プロジェクト: dozsolti/Facultate
        public static Floatz Pow(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();

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

            /*
             * 1,5
             * ^1 + ^0,5
             */
            Floatz c = Floatz.Constants.ONE();

            if (!b.isIntreagaZero())
            {
                b  = b.ToIntreaga();
                c  = new Floatz(a);
                b -= Floatz.Constants.ONE();
                while (!b.isZero())
                {
                    c *= a;

                    b -= Floatz.Constants.ONE();
                    //Console.WriteLine("{0}>{1}",b.ToString(),Floatz.Constants.ZERO());
                }
            }
            if (!refB.isZecimalaZero())
            {
                Floatz zec = refB.Clone().ToZecimala();

                Floatz n = (Floatz.Constants.ONE() / zec);
                //Console.WriteLine(zec.ToString());
                //Console.WriteLine(n.ToString());

                Floatz xn  = a.Clone();
                Floatz xn1 = xn - (Pow(xn, n) - a) / (n * Pow(xn, (n - Floatz.Constants.ONE())));
                Floatz dif = xn - xn1;
                while (Floatz.Abs(dif) > Floatz.Constants.SqrtTreshold)
                {
                    Floatz powXnNM1 = Pow(xn, (n - Floatz.Constants.ONE()));
                    xn1 = xn - ((powXnNM1 * xn) - a) / (n * powXnNM1);
                    dif = xn - xn1;
                    //Console.WriteLine("xn1: {0}\nxn:{1}",xn1,xn);
                    xn = xn1.Clone();
                }
                c *= xn1;
            }
            return(c);
        }
コード例 #2
0
ファイル: FloatzOperators.cs プロジェクト: dozsolti/Facultate
        public static Floatz Add(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();
            Floatz c = new Floatz(a.separator);

            c.isNegative = false;

            if (a.isZero() && b.isZero())
            {
                return(Floatz.Constants.ZERO(a.separator));
            }
            if (a.isZero())
            {
                return(b);
            }
            if (b.isZero())
            {
                return(a);
            }
            #region In caz ca trebuie substrase
            if (a.isNegative && !b.isNegative)
            {
                //-a + b => b-a
                //-3 + 5 = 5-3 = 2
                return(Subtract(b, a));
            }
            if (!a.isNegative && b.isNegative)
            {
                //a + (-b)=> a-b
                //5 + -3 = 2
                return(Subtract(a, b));
            }
            if (a.isNegative && b.isNegative)
            {
                //-a + -b=> -(a+b)
                //-5 + -3 = -(5+3)=-8
                c.isNegative = true;
            }
            #endregion

            c.zecimala = Add2Lists(a.zecimala, b.zecimala, "zecimala");
            c.intreaga = Add2Lists(a.intreaga, b.intreaga, "intreaga");

            if (c.zecimala.Count > Math.Max(a.zecimala.Count, b.zecimala.Count))
            {
                c.intreaga = Add2Lists(c.intreaga, new List <int> {
                    1
                });
                c.zecimala.RemoveAt(0);
            }

            c.ComprimaZecimala();

            return(c);
        }
コード例 #3
0
ファイル: FloatzOperators.cs プロジェクト: dozsolti/Facultate
        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);
        }
コード例 #4
0
ファイル: FloatzOperators.cs プロジェクト: dozsolti/Facultate
        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);
        }
コード例 #5
0
ファイル: FloatzOperators.cs プロジェクト: dozsolti/Facultate
        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);
        }