Ejemplo n.º 1
0
        /// <summary>
        /// Postbinary numeric addition
        /// </summary>
        /// <param name="leftOperand">Left operand of operation.</param>
        /// <param name="rightOperand">Right operand of operation.</param>
        /// <returns>Result of operation</returns>
        private PBNumber ADD(PBNumber leftOperand, PBNumber rightOperand)
        {
            PBNumber opA = (PBNumber)leftOperand.Clone();
            PBNumber opB = (PBNumber)rightOperand.Clone();
            PBNumber opC = new PBNumber("0", IPBNumber.NumberCapacity.PB128, IPBNumber.RoundingType.POST_BINARY);

            String iuA = "";
            String iuB = "";

            switch (tCMP(opA.Exponent, opB.Exponent)) // Exponent align
                {
                    case 1:
                        opB = ExponentAlign(opA, opB);
                        // log here A, B
                        iuA = "1";
                        iuB = "";
                        break;
                    case -1:
                        opA = ExponentAlign(opB, opA);
                        // log here A, B
                        iuA = "";
                        iuB = "1";
                        break;
                    case 0:
                    default:
                        // log here A, B
                        break;
                }

            PBConvertion pbconvertion = new PBConvertion();
            int a = Int32.Parse(pbconvertion.convert2to10IPart(opA.Exponent));

            String str = tADD(iuA + opA.Mantissa, iuB + opB.Mantissa, false);
            int iuPosition = str.IndexOf('1');
            a -= iuPosition;
            str = str.Substring(iuPosition);

            opC.Mantissa = str.Substring(1);

            opC.Exponent = pbconvertion.convert10to2IPart(a.ToString());
            // log here C
            return opC;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Shifts PBNumber number with regard to shiftValue sign(shift direction) and modulus(counts of shifts).
 /// </summary>
 /// <param name="operand">PBNumber to shift.</param>
 /// <param name="shiftValue">Sign shows direction of shift; Modulus shows counts of shifts.</param>
 /// <returns>Shifted PBNumber</returns>
 public static PBNumber Shift(PBNumber operand, int shiftValue)
 {
     PBConvertion pbconvertion = new PBConvertion();
     int expA = Int32.Parse(pbconvertion.convert2to10IPart(operand.Exponent));
     expA += shiftValue;
     IPBNumber.IFPartsOfNumber ipbn = new IPBNumber.IFPartsOfNumber();
     if (shiftValue != 0)
     {
         if (shiftValue > 0)
         {
             String newMantissa = AddSymbols("0", "1" + operand.Mantissa, shiftValue - 1, true);
             operand.Mantissa = newMantissa;
         }
         else
         {
             String precipitated = operand.Mantissa.Substring(operand.Mantissa.Length + shiftValue);
             operand.Mantissa = AddSymbols("0", operand.Mantissa, Math.Abs(shiftValue), true);
             if (precipitated.Length >= 1)
             {
                 operand.Round(IPBNumber.RoundingType.POST_BINARY, operand.Mantissa, 104, ipbn, 0, IPBNumber.NumberCapacity.PB128);
             }
         }
     }
     operand.Exponent = pbconvertion.convert10to2IPart(expA.ToString());
     return operand;
 }