public static MiniFloat operator -(MiniFloat Minuend, MiniFloat Subtrahend) { MiniFloat m = Minuend.Copy(); MiniFloat n = Subtrahend.Copy(); /* To subtract, you add the negative */ n.Signbit = (n.Signbit == '0') ? '1' : '0'; //flip dat bit return(m + n); //+ operator deals with the signbit stuff }
public static Binary operator -(Binary Minuend, Binary Subtrahend) { Binary m = Minuend.Copy(); Binary n = Subtrahend.Copy(); void Borrow(ref Binary x, int p /* The index that caused a problem & must become a two */) { while (x[p] != '1' && x[p] != '2') { for (int i = p - 1 /**/; i >= 0; i--) { if (x[i] == '1') { x[i] = '0'; x[i + 1] = '2'; break; } else if (x[i] == '2') { x[i] = '1'; x[i + 1] = '2'; break; } } } } 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--) { bool flag = true; do { int x = MainClass.ValueOf(m[i]) - MainClass.ValueOf(n[i]); if (x < 0) { if (i - 1 < 0) { throw new BinaryUnderFlowException("Cannot borrow in last column, underflow error."); } else { Borrow(ref m, i); } } else { flag = false; c[i] = MainClass.Character(x); } } while (flag); } return(c); } public static Binary operator *(Binary Multiplicand, Binary Multiplier) { Binary m = Multiplicand.Copy(); Binary n = Multiplier.Copy(); Binary[] partials = new Binary[n.Length]; // List of the partial sums for (int i = 0; i < partials.Length; i++) { partials[i] = new Binary(); } for (int i = n.Length - 1; i >= 0; i--) { for (int k = 0; k < n.Length - (i + 1); k++) { partials[i] += '0'; } for (int j = m.Length - 1; j >= 0; j--) { partials[i] += Convert.ToString(MainClass.ValueOf(n[i]) * MainClass.ValueOf(m[j])); } partials[i].Reverse(); } Binary c = new Binary(); for (int i = 0; i < partials.Length; i++) { c += partials[i]; } return(c); } public static Binary operator *(int Multiplicand, Binary Multiplier) => new Binary(BaseChange(Multiplicand, 10, 2)) * Multiplier; public static Binary operator *(Binary Multiplicand, int Multiplier) => Multiplicand * new Binary(BaseChange(Multiplier, 10, 2)); public static Binary operator /(Binary Dividend /* m */, Binary Divisor /* n */) { Binary m = Dividend.Copy(); Binary n = Divisor.Copy(); /* Get the first set of digits of m where that set is the same length as n's digits */ Binary quotient = new Binary(); //if N is 3 digits long, quotient starts with 000. Binary drop = new Binary(); for (int i = 0; i < m.Length; i++) { drop += m[i]; if (n <= drop) { quotient += '1'; } else { quotient += '0'; } drop = drop - n * MainClass.ValueOf(quotient[i]) /*0 or 1*/; } if (quotient.Body.IndexOf('1') != -1) //If there's a one in it { while (quotient[0] != '1') { quotient.Body = quotient.Body.Substring(1); } } return(quotient); } /* End of Operators */ }