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

            return(!(a == b));
        }
Example #2
0
        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);
        }
Example #3
0
            public static string TestImpartire()
            {
                object[,] testeComparari = new object[, ] {
                    { "4", "2", "2" },
                    { "355", "113", "3,14159292035398230088" },
                    { "6", "2", "3" }
                };

                string str = "";

                for (int i = 0; i < testeComparari.GetLength(0); i++)
                {
                    Floatz a             = new Floatz((string)testeComparari[i, 0]);
                    Floatz b             = new Floatz((string)testeComparari[i, 1]);
                    Floatz resultCorrect = new Floatz((string)testeComparari[i, 2]);
                    Floatz c             = a / b;

                    if (c != resultCorrect)
                    {
                        str += String.Format(
                            "\n Test failed:Impartire #{0} nereusita.\n \n {1}/{2}={3} |corect e: {4}\n\n",
                            i, a.ToString(), b.ToString(), c.ToString(), resultCorrect.ToString()
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la impartire.");
                }
                return(str);
            }
Example #4
0
        public static bool operator ==(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();

            if (a.isNegative != b.isNegative)
            {
                return(false);
            }

            int compIntreaga = Compare2Lists(a.intreaga, b.intreaga, "intreaga");

            if (compIntreaga != 0)
            {
                return(false);
            }

            int compZecimala = Compare2Lists(a.zecimala, b.zecimala, "zecimala");

            if (compZecimala != 0)
            {
                return(false);
            }

            return(true);
        }
Example #5
0
        public Floatz ToIntreaga()
        {
            Floatz a = new Floatz();

            a.intreaga.Clear();
            a.intreaga.AddRange(this.intreaga);
            return(a);
        }
Example #6
0
        public Floatz ToZecimala()
        {
            Floatz a = new Floatz();

            a.zecimala.Clear();
            a.zecimala.AddRange(this.zecimala);
            return(a);
        }
Example #7
0
        public static List <int> DeCateOriIntra(List <int> b, List <int> temp)
        {
            if (Floatz.ListIsZero(temp))
            {
                return new List <int> {
                           0
                }
            }
            ;
            temp = Floatz.ComprimaList(temp, "intreaga");
            if (Floatz.Compare2Lists(temp, b) == 1)
            {
                return new List <int> {
                           0
                }
            }
            ;
            if (Floatz.Compare2Lists(temp, b) == 0)
            {
                return new List <int> {
                           1
                }
            }
            ;


            List <int> _b = new List <int>();

            foreach (int item in b)
            {
                _b.Add(item);
            }

            List <int> i = new List <int> {
                0
            };
            int cmp = Floatz.Compare2Lists(temp, _b);

            while (cmp == -1)
            {
                i = Floatz.Add2Lists(i, new List <int> {
                    1
                });
                _b.Clear();
                _b  = Floatz.Multiply2Lists(b, i);
                cmp = Floatz.Compare2Lists(temp, _b);
            }
            if (cmp != 0)
            {
                i = Floatz.Subtract2Lists(i, new List <int> {
                    1
                });
            }
            i = ComprimaList(i);
            return(i);
        }
Example #8
0
        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);
        }
Example #9
0
        public static Floatz Abs(Floatz refA)
        {
            Floatz a = new Floatz(refA);

            if (!refA.isNegative)
            {
                a.isNegative = false;
            }
            return(a);
        }
Example #10
0
            public static string TestComparari()
            {
                object[,] testeComparari = new object[, ] {
                    { "0", '<', "1", true },
                    { "1", '<', "0", false },
                    { "-1", '>', "2", false },
                    { "-1", '<', "2", true },
                    { "-1", '<', "-2", false },
                    { "-1", '>', "-2", true },
                    { "-1", '<', "0", true },
                    { "3,14159265358979323846264338327950", '>', "2,71828182845904523536028747135266", true },
                    { "-1.123123", '<', "-1.123023", true },
                    { "1,32", '>', "1,12", true },
                    { "1,2", '>', "0,2", true },
                    { "1,32", '<', "1,12", false },
                    { "1,2", '<', "0,2", false },
                    { "-1,2", '<', "1", true },
                    { "-1,2", '>', "1", false },
                    { "1", '<', "-1,2", false },
                    { "1", '>', "-1,2", true },
                    { "1,0000000000000000000", '>', "1", false },
                    { "1,0000000000000000000", '<', "1", false },
                    { "9", '<', "355", true },
                    { "355", '>', "9", true }
                };

                string str = "";

                for (int i = 0; i < testeComparari.GetLength(0); i++)
                {
                    Floatz a      = new Floatz((string)testeComparari[i, 0]);
                    Floatz b      = new Floatz((string)testeComparari[i, 2]);
                    bool   result = (bool)testeComparari[i, 3];
                    char   type   = (char)testeComparari[i, 1];
                    if (type == '<')
                    {
                        if ((a < b) != result)
                        {
                            str += String.Format("\n Test failed:Compararea #{2} nereusita.\n \n {3}<{4}={0} |corect e: {1}\n\n", (a < b), result, i, a.ToString(), b.ToString());
                        }
                    }
                    else
                    {
                        if ((a > b) != result)
                        {
                            str += String.Format("\n Test failed:Compararea #{2} nereusita.\n \n {3}>{4}={0} |corect e: {1}\n\n", (a > b), result, i, a.ToString(), b.ToString());
                        }
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la comparari.");
                }
                return(str);
            }
Example #11
0
        public Floatz(Floatz n)
        {
            this.isNegative = n.isNegative;
            this.separator  = n.separator;

            this.intreaga = new List <int>();
            this.intreaga.AddRange(n.intreaga);

            this.zecimala = new List <int>();
            this.zecimala.AddRange(n.zecimala);
        }
Example #12
0
            public static string TestDeCateOriIntra()
            {
                object[,] teste = new object[, ] {
                    { "0", "0", "0" },
                    { "1", "1", "1" },
                    { "1", "2", "2" },
                    { "1", "3", "3" },
                    { "2", "4", "2" },
                    { "6", "12", "2" },
                    { "24", "12", "0" },
                    { "113", "355", "3" },
                    { "456", "1368", "3" },
                    { "74916", "674244", "9" },
                    { "44", "1242", "28" },
                    { "51", "500", "9" }
                };

                string str = "";

                for (int i = 0; i < teste.GetLength(0); i++)
                {
                    List <int> b = new List <int>();

                    for (int j = 0; j < ((string)teste[i, 0]).Length; j++)
                    {
                        b.Add(Floatz.charToInt(((string)teste[i, 0])[j]));
                    }

                    List <int> temp = new List <int>();
                    for (int j = 0; j < ((string)teste[i, 1]).Length; j++)
                    {
                        temp.Add(Floatz.charToInt(((string)teste[i, 1])[j]));
                    }
                    List <int> resultCorrect = new List <int>();
                    for (int j = 0; j < ((string)teste[i, 2]).Length; j++)
                    {
                        resultCorrect.Add(Floatz.charToInt(((string)teste[i, 2])[j]));
                    }

                    List <int> result = Floatz.DeCateOriIntra(b, temp);
                    if (Floatz.Compare2Lists(result, resultCorrect) != 0)
                    {
                        str += String.Format(
                            "\n Test failed:DeCateOriIntra #{0} nereusita.\n \n b={1},temp={2}, result={3} |corect e: {4}\n\n",
                            i, Floatz.ListToString(b), Floatz.ListToString(temp), Floatz.ListToString(result), Floatz.ListToString(resultCorrect)
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la DeCateOriIntra.");
                }
                return(str);
            }
Example #13
0
 private static Floatz CalcGoldenRation(int i)
 {
     if (i == 0)
     {
         return(Floatz.Constants.ONE());
     }
     return(Floatz.Constants.ONE()
            + Floatz.Divide(
                Floatz.Constants.ONE(),
                CalcGoldenRation(i - 1),
                80));
 }
Example #14
0
        static void Main(string[] args)
        {
            //Decimal x = Decimal.Divide(355m, 113m);
            // bvhg Console.WriteLine(x);
            //Floatz.Divide(new Floatz(355), new Floatz(113), 700).Print();

            Console.ReadKey();
            Console.WriteLine("Golden ratio:");
            Floatz d = CalcGoldenRation(200);

            d.Print("Floatz: ");
            Console.WriteLine("Corect: 1,61803398874989484820458683436563811772030917980576286213544862270526046281890244...");
            Console.ReadKey();

            //2^100
            Console.WriteLine("Puterile lui 2:");
            Floatz g = new Floatz(2);

            for (int i = 1; i < 7000; i++)
            {
                g.Print("2^" + i + ":");
                g *= Floatz.Constants.TWO();
            }


            /*
             * Floatz a = new Floatz(3);
             * Floatz b = new Floatz(4)* new Floatz(3)* new Floatz(2);
             * bool add = true;
             *
             * Console.WriteLine("Aproximatia lui Pi:");
             *
             * for (int i = 2; i < 60000; i++)
             * {
             *  if (add)
             *      a += Floatz.Divide(new Floatz(4), b, 30);
             *  else
             *      a -= Floatz.Divide(new Floatz(4), b, 30);
             *  add = !add;
             *  b = new Floatz(2*i) * (new Floatz(2*i+1)) * (new Floatz(2 * i + 2));
             * }
             * Console.WriteLine("Math.PI: {0}", Math.PI);
             * a.Print("Floatz : ");
             * Console.WriteLine("     PI: 3,1415926535897932384626433832795...");
             * Console.ReadKey();*/

            Console.ReadKey();
        }
Example #15
0
        public static List <int> Subtract2Lists(List <int> refA, List <int> refB)
        {
            List <int> a = new List <int>();

            a.AddRange(refA);
            List <int> b = new List <int>();

            b.AddRange(refB);
            List <int> c = new List <int>();

            a.Reverse();
            b.Reverse();
            //a va fii lista cu cele mai multe numere
            if (a.Count < b.Count)
            {
                List <int> temp = a;
                a = b;
                b = temp;
            }
            int carry = 0;

            for (int i = 0; i < b.Count; i++)
            {
                int nr = a[i] - b[i] - carry;
                if (nr < 0)
                {
                    carry = 1;
                    nr   += 10;
                }
                else
                {
                    carry = 0;
                }
                c.Add(nr);
            }


            for (int i = b.Count; i < a.Count; i++)
            {
                c.Add(a[i] - carry);
                carry = 0;
            }


            c.Reverse();
            c = Floatz.ComprimaList(c);
            return(c);
        }
Example #16
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 #17
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 #18
0
            public static string TestAdunare()
            {
                object[,] teste = new object[, ] {
                    { "0", "0", "0" },
                    { "1", "1", "2" },
                    { "1", "2", "3" },
                    { "1", "3", "4" },
                    { "2", "4", "6" },
                    { "6", "12", "18" },
                    { "24", "12", "36" },
                    { "113", "355", "468" },
                    { "0,5", "0,16", "0,66" },
                    { "0,16", "0,5", "0,66" },
                    { "0,5", "0,166", "0,666" },
                    { "0,00416666", "0,66666666", "0,67083332" }
                };

                string str = "";

                for (int i = 0; i < teste.GetLength(0); i++)
                {
                    Floatz a             = new Floatz((string)teste[i, 0]);
                    Floatz b             = new Floatz((string)teste[i, 1]);
                    Floatz resultCorrect = new Floatz((string)teste[i, 2]);

                    Floatz result = a + b;

                    if (result != resultCorrect)
                    {
                        str += String.Format(
                            "\n Test failed:Adunare #{0} nereusita.\n \n {1}+\n {2}=\n {3} |corect e: {4}\n\n",
                            i, a, b, result, resultCorrect
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la Adunare.");
                }
                return(str);
            }
Example #19
0
            public static string TestPow()
            {
                object[,] testeComparari = new object[, ] {
                    { "0", "9", "0,0" },
                    { "1", "0", "1,0" },
                    { "1", "1", "1,0" },
                    { "2", "1", "2,0" },
                    { "2", "3", "8,0" },
                    { "2", "10", "1024,0" },
                    { "20", "9", "512000000000,0" },
                    { "4", "0,5", "2" },
                    { "256", "0,25", "4" },
                    { "4", "0,5", "2" },
                    { "256", "0,4", "4" },
                    { "2", "0,5", "1" }
                };

                string str = "";

                for (int i = 0; i < testeComparari.GetLength(0); i++)
                {
                    Floatz a = new Floatz((string)testeComparari[i, 0]);
                    Floatz b = new Floatz((string)testeComparari[i, 1]);
                    //Console.WriteLine("{0}->{1}",b.ToString(),b.isOne());
                    string resultCorrect = (string)testeComparari[i, 2];
                    Floatz c             = Floatz.Pow(a, b);
                    if (c.ToString() != resultCorrect)
                    {
                        str += String.Format(
                            "\n Test failed:Putere #{0} nereusita.\n \n Pow({1};{2})={3} |corect e: {4}\n\n",
                            i, a.ToString(), b.ToString(), c.ToString(), resultCorrect
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la putere.");
                }
                return(str);
            }
Example #20
0
            public static string TestScadere()
            {
                object[,] testeComparari = new object[, ] {
                    { "0", "0", "0,0" },
                    { "0", "1", "-1,0" },
                    { "1", "0", "1,0" },
                    { "2", "1", "1,0" },
                    { "1,32", "1,12", "0,2" },
                    { "1,2", "1", "0,2" },
                    { "-1,2", "1,0", "-2,2" },
                    { "1", "-1,2", "2,2" },
                    { "3,46666", "0,57", "2,89666" },
                    { "10", "1", "9,0" },
                    { "101", "2", "99,0" },
                    { "204,8900000011920928955078125", "256", "-51,1099999" }
                };

                string str = "";

                for (int i = 0; i < testeComparari.GetLength(0); i++)
                {
                    Floatz a             = new Floatz((string)testeComparari[i, 0]);
                    Floatz b             = new Floatz((string)testeComparari[i, 1]);
                    string resultCorrect = (string)testeComparari[i, 2];
                    Floatz c             = a - b;
                    if (c.ToString() != resultCorrect)
                    {
                        str += String.Format(
                            "\n Test failed:Scaderea #{0} nereusita.\n \n {1}-{2}={3} |corect e: {4}\n\n",
                            i, a.ToString(), b.ToString(), c.ToString(), resultCorrect
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la scadere.");
                }
                return(str);
            }
Example #21
0
            public static string TestInmultire()
            {
                object[,] testeComparari = new object[, ] {
                    { "0,0", "0,0", "0,0" },
                    { "0", "1", "0,0" },
                    { "1", "1", "1,0" },
                    { "2", "1", "2,0" },
                    { "2", "2", "4,0" },
                    { "2", "6,2", "12,4" },
                    { "384,1789454", "-1", "-384,1789454" },
                    { "3,1415", "3,1415", "9,86902225" },
                    { "31415", "31415", "986902225" },
                    { "384,1789454", "98,87546", "37985,869948739884" },
                    { "456", "2", "912,0" }
                };

                string str = "";

                for (int i = 0; i < testeComparari.GetLength(0); i++)
                {
                    Floatz a             = new Floatz((string)testeComparari[i, 0]);
                    Floatz b             = new Floatz((string)testeComparari[i, 1]);
                    Floatz resultCorrect = new Floatz((string)testeComparari[i, 2]);
                    Floatz c             = Floatz.Multiply(a, b);

                    if (c != resultCorrect)
                    {
                        str += String.Format(
                            "\n Test failed:Inmultirea #{0} nereusita.\n \n {1}*{2}={3} |corect e: {4}\n\n",
                            i, a.ToString(), b.ToString(), c.ToString(), resultCorrect.ToString()
                            );
                    }
                }
                if (str == "")
                {
                    return("Nu exista probleme la inmultirea.");
                }
                return(str);
            }
Example #22
0
        public static Floatz FromOneList(List <int> a, int separtorPlace)
        {
            int i = a.Count - separtorPlace;

            List <int> intreaga = new List <int>(), zecimala = new List <int>();

            if (separtorPlace <= 0)
            {
                intreaga.AddRange(a);
                for (int j = 0; j < -separtorPlace; j++)
                {
                    intreaga.Add(0);
                }
                zecimala = new List <int>()
                {
                    0
                };
            }
            else if (separtorPlace >= a.Count)
            {
                intreaga = new List <int> {
                    0
                };
                zecimala = new List <int>();
                for (int j = 0; j < separtorPlace - a.Count; j++)
                {
                    zecimala.Add(0);
                }
                zecimala.AddRange(a);
            }
            else
            {
                intreaga.AddRange(a.GetRange(0, i));
                zecimala.AddRange(a.GetRange(i, a.Count - i));
            }
            Floatz b = new Floatz(intreaga, zecimala, false);

            return(b);
        }
Example #23
0
        public static Floatz DivideFloatzWithList(Floatz a, List <int> b, long nrMaxZecimalaLaImpartire, bool debugMode = false)
        {
            Floatz     c    = new Floatz();
            List <int> temp = new List <int>();
            int        cmp  = 0;

            for (int i = 0; i < a.intreaga.Count; i++)
            {
                temp.Add(a.intreaga[i]);
                cmp = Compare2Lists(temp, b);
                if (cmp < 1)//317/51 || 317 || 51
                {
                    List <int> deCateOriIntra = DeCateOriIntra(b, temp);
                    List <int> sub            = Floatz.Multiply2Lists(deCateOriIntra, b);
                    if (debugMode)
                    {
                        Console.WriteLine(Floatz.ListToString(temp) + "/" + Floatz.ListToString(b) + "=" + Floatz.ListToString(deCateOriIntra));
                    }

                    c.intreaga.AddRange(deCateOriIntra);
                    if (debugMode)
                    {
                        Console.Write(Floatz.ListToString(temp) + "-" + Floatz.ListToString(sub) + "=");
                    }
                    temp = Floatz.Subtract2Lists(temp, sub);
                    if (debugMode)
                    {
                        Console.Write(Floatz.ListToString(temp));
                        Console.WriteLine();
                    }

                    if (Floatz.ListIsZero(temp))
                    {
                        break;
                    }
                }
                else
                {
                    c.intreaga.Add(0);
                }
            }
            c.ComprimaIntreaga();
            if (nrMaxZecimalaLaImpartire == 0)
            {
                return(c);
            }
            c.zecimala.Clear();
            if (Floatz.ListIsZero(temp))
            {
                temp.Clear();
            }
            if (debugMode)
            {
                Console.WriteLine(",");
            }

            for (int i = 0; i < a.zecimala.Count && nrMaxZecimalaLaImpartire >= 0; i++)
            {
                temp.Add(a.zecimala[i]);

                cmp = Compare2Lists(temp, b);
                if (cmp < 1)
                {
                    List <int> deCateOriIntra = DeCateOriIntra(b, temp);
                    List <int> sub            = Floatz.Multiply2Lists(deCateOriIntra, b);
                    if (debugMode)
                    {
                        Console.WriteLine(Floatz.ListToString(temp) + "/" + Floatz.ListToString(b) + "=" + Floatz.ListToString(deCateOriIntra));
                    }

                    c.zecimala.AddRange(deCateOriIntra.GetRange(0, Math.Min(deCateOriIntra.Count, (int)nrMaxZecimalaLaImpartire)));
                    nrMaxZecimalaLaImpartire -= Math.Min(deCateOriIntra.Count, (int)nrMaxZecimalaLaImpartire);
                    if (debugMode)
                    {
                        Console.WriteLine("nrMaxZecimalaLaImpartire:" + nrMaxZecimalaLaImpartire);
                        Console.Write(Floatz.ListToString(temp) + "-" + Floatz.ListToString(sub) + "=");
                    }

                    temp = Floatz.Subtract2Lists(temp, sub);
                    cmp  = Compare2Lists(temp, b);
                    if (cmp == 1)
                    {
                        temp.Add(0);
                    }
                    if (debugMode)
                    {
                        Console.Write(Floatz.ListToString(temp));
                        Console.WriteLine();
                    }
                }
                else
                {
                    c.zecimala.Add(0);
                    nrMaxZecimalaLaImpartire--;
                }
            }
            if (!Floatz.ListIsZero(temp))
            {
                if (debugMode)
                {
                    Console.WriteLine("---start restu---");
                }
                if (nrMaxZecimalaLaImpartire > 0)
                {
                    if (cmp == 1)
                    {
                        temp.Add(0);
                    }
                    while (nrMaxZecimalaLaImpartire >= 0)//for (long i = nrMaxZecimalaLaImpartire; i >= 0; i--)
                    {
                        if (debugMode)
                        {
                            Console.WriteLine(Floatz.ListToString(temp));
                        }
                        cmp = Compare2Lists(temp, b);
                        if (debugMode)
                        {
                            Console.WriteLine(Floatz.ListToString(temp) + "?" + Floatz.ListToString(b) + "=" + cmp);
                        }
                        if (cmp < 1)
                        {
                            List <int> deCateOriIntra = DeCateOriIntra(b, temp);
                            List <int> sub            = Floatz.Multiply2Lists(deCateOriIntra, b);
                            if (debugMode)
                            {
                                Console.WriteLine(Floatz.ListToString(temp) + "/" + Floatz.ListToString(b) + "=" + Floatz.ListToString(deCateOriIntra));
                            }

                            c.zecimala.AddRange(deCateOriIntra.GetRange(0, Math.Min(deCateOriIntra.Count, (int)nrMaxZecimalaLaImpartire)));
                            nrMaxZecimalaLaImpartire -= Math.Min(deCateOriIntra.Count, (int)nrMaxZecimalaLaImpartire);
                            if (nrMaxZecimalaLaImpartire == 0)
                            {
                                break;
                            }
                            if (debugMode)
                            {
                                Console.Write(Floatz.ListToString(temp) + "-" + Floatz.ListToString(sub) + "=");
                            }
                            temp = Floatz.Subtract2Lists(temp, sub);
                            cmp  = Compare2Lists(temp, b);
                            if (cmp == 1)
                            {
                                temp.Add(0);
                            }
                            if (debugMode)
                            {
                                Console.Write(Floatz.ListToString(temp));
                                Console.WriteLine();
                            }
                            if (Floatz.ListIsZero(temp))
                            {
                                break;
                            }
                        }
                        else
                        {
                            if (debugMode)
                            {
                                Console.WriteLine("0 was added.");
                            }
                            c.zecimala.Add(0);
                            temp.Add(0);
                            nrMaxZecimalaLaImpartire--;
                        }
                    }
                }
            }
            if (debugMode)
            {
                Console.WriteLine(".");
            }
            c.ComprimaZecimala();
            return(c);
        }
Example #24
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);
        }
Example #25
0
 public static void PrintList(List <int> a, string prefix = "")
 {
     Console.WriteLine(prefix + Floatz.ListToString(a));
 }
Example #26
0
        public static bool operator <(Floatz refA, Floatz refB)
        {
            Floatz a = refA.Clone();
            Floatz b = refB.Clone();

            if (!a.isNegative && b.isNegative)
            {
                return(false);
            }
            if (a.isNegative && !b.isNegative)
            {
                return(true);
            }

            if (a.isNegative && b.isNegative)
            {
                // -0.445 > -0.5 => true
                int compIntreaga = Compare2Lists(a.intreaga, b.intreaga, "intreaga");
                if (compIntreaga == 1)
                {
                    return(false);
                }
                if (compIntreaga == -1)
                {
                    return(true);
                }
                else
                {
                    int compZecimala = Compare2Lists(a.zecimala, b.zecimala, "zecimala");
                    if (compZecimala == 1)
                    {
                        return(false);
                    }
                    if (compZecimala == -1)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                int compIntreaga = Compare2Lists(a.intreaga, b.intreaga, "intreaga");
                if (compIntreaga == 1)
                {
                    return(true);
                }
                if (compIntreaga == -1)
                {
                    return(false);
                }
                else
                {
                    int compZecimala = Compare2Lists(a.zecimala, b.zecimala, "zecimala");
                    if (compZecimala == 1)
                    {
                        return(true);
                    }
                    if (compZecimala == -1)
                    {
                        return(false);
                    }
                }
            }
            return(false);
        }