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); }
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); }
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); }
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); }