/// <summary>
        /// Returns aligned rightOperand relative to leftOperand.
        /// IMPORTANT: leftOperand must be greater or equal (>=) to rightOperand
        /// </summary>
        /// <param name="leftOperand">Operand, relative to which rightOperand will be aligned.</param>
        /// <param name="rightOperand">Operand, that will be aligned</param>
        /// <returns>Aligned operand.</returns>
        public static PBNumber ExponentAlign(PBNumber leftOperand, PBNumber rightOperand)
        {
            PBConvertion pbconvertion = new PBConvertion();
            int leftExponentValue = Int32.Parse(pbconvertion.convert2to10IPart(leftOperand.Exponent));
            int rightExponentValue = Int32.Parse(pbconvertion.convert2to10IPart(rightOperand.Exponent));

            return Shift(rightOperand, leftExponentValue - rightExponentValue);
        }
        public PBNumber(String inDigit, NumberCapacity inCapacity, RoundingType inRounding)
            : this(inCapacity)
        {
            String currentNumber = "";
            this.InitValue = inDigit;

            IPBNumber.IFPartsOfNumber currentPartialNumber;
            IPBNumber.IFPartsOfNumber currentPartialNumber2cc;
            if (pbConvertion == null)
                pbConvertion = new PBConvertion();
            currentNumber = pbConvertion.NormalizeNumber(inDigit, PBConvertion.ACCURANCY, IPBNumber.NumberFormat.INTEGER);
            currentPartialNumber = pbConvertion.DenormalizeNumber(currentNumber, IPBNumber.NumberFormat.INTEGER);

            currentPartialNumber2cc.Sign = currentPartialNumber.Sign;
            currentPartialNumber2cc.IntegerPart = pbConvertion.convert10to2IPart(currentPartialNumber.IntegerPart);
            currentPartialNumber2cc.FloatPart = pbConvertion.convert10to2FPart(currentPartialNumber.FloatPart);

            //Define Exponent before Mantissa for correct running algorithm
            selectExp(currentPartialNumber2cc, inCapacity, IPBNumber.NumberFormat.INTEGER);
            selectMantissa(currentPartialNumber2cc, inCapacity, IPBNumber.NumberFormat.INTEGER, inRounding);
            this.Sign = currentPartialNumber.Sign;
        }
 public PBNumber(NumberCapacity inWidth, String inSign, String inExponent, String inMantissa, RoundingType inRounding)
 {
     this.width = inWidth;
     switch (this.width)
     {
         case NumberCapacity.PB32:
             {
                 this.offset = NumberOffset.PB32;
                 this.exponentLenght = NumberExponentLength.PB32;
                 this.mantissaLenght = NumberMantissaLength.PB32;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB32];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB32];
                 break;
             }
         case NumberCapacity.PB64:
             {
                 this.offset = NumberOffset.PB64;
                 this.exponentLenght = NumberExponentLength.PB64;
                 this.mantissaLenght = NumberMantissaLength.PB64;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB64];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB64];
                 break;
             }
         case NumberCapacity.PB128:
             {
                 this.offset = NumberOffset.PB128;
                 this.exponentLenght = NumberExponentLength.PB128;
                 this.mantissaLenght = NumberMantissaLength.PB128;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB128];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB128];
                 break;
             }
         case NumberCapacity.PB256:
             {
                 this.offset = NumberOffset.PB256;
                 this.exponentLenght = NumberExponentLength.PB256;
                 this.mantissaLenght = NumberMantissaLength.PB256;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB256];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB256];
                 break;
             }
         default:
             {
                 break;
             }
     }
     this.name = "Name-" + this.width.ToString() + "[" + inSign + inExponent + "|" + inMantissa + "]";
     this.sign = inSign;
     this.Exponent = inExponent;
     this.Mantissa = inMantissa;
     this.roundingType = inRounding;
     if (pbConvertion == null)
         pbConvertion = new PBConvertion();
 }
 public PBNumber(PBPrototype inNumberPrototype, NumberCapacity inCapacity, RoundingType inRounding)
     : this(inCapacity, inNumberPrototype.Sign, inNumberPrototype.Exponent, inNumberPrototype.Mantissa, inRounding)
 {
     if (pbConvertion == null)
         pbConvertion = new PBConvertion();
 }
        /// <summary>
        /// Based on Number Exp and Mantisa function calculates Correct Value 
        /// Uses: Number.E, Number.M
        /// IMPORTANT- Doesn't count variety of formats (only Integer)
        /// </summary>
        /// <param name="scientificNotation">true - returns digit in scientific notation, else - in simple format</param>
        /// <returns>Returns digit accordingly parameters</returns>
        public string toDigit(bool scientificNotation)
        {
            String CorrectResult = "";
            String CorrectResultExp = "";

            try
            {
                int Offset = 0;
                int precision = 1900;
                String Sign = "";
                // temp Vars
                IPBNumber.stateOfNumber currentState;
                IPBNumber.NumberFormat NumberFormat = IPBNumber.NumberFormat.INTEGER;
                PBConvertion pbconvertion = new PBConvertion();
                switch (this.Name)
                {
                    case "Num32": precision = 1000; break;
                    case "Num64": precision = 1200; break;
                    case "Num128": precision = 1800; break;
                    case "Num256": precision = 1900; break;
                    default: precision = 1800; break;
                }
                switch (NumberFormat)
                {
                    case 0: Offset = (int)this.Offset; break;
                }
                currentState = this.NumberState;

                switch (currentState)
                {
                    case IPBNumber.stateOfNumber.NORMALIZED:
                        CorrectResult = pbconvertion.calcResForNorm(this, precision);
                        break;
                    default:
                        pbconvertion.calcResForNan(this);
                        break;
                }
                IPBNumber.stateOfNumber tempState = this.NumberState;
                Sign = this.Sign;

                if ((tempState == IPBNumber.stateOfNumber.NORMALIZED) || (tempState == IPBNumber.stateOfNumber.DENORMALIZED))
                {
                    if (NumberFormat == 0)
                    {
                        if (CorrectResult.IndexOf(':') == -1)
                            CorrectResultExp = pbconvertion.convertToExp(CorrectResult);
                        else
                        {
                            CorrectResultExp = pbconvertion.convertToExp(CorrectResult.Split(':')[0]);
                            CorrectResultExp += ":" + pbconvertion.convertToExp(CorrectResult.Split(':')[1]);
                        }
                    }
                }

                String result = (scientificNotation) ? CorrectResultExp : CorrectResult;
                return signCharacter + result;
            }
            catch (Exception ex)
            {
                throw new FCCoreArithmeticException("Func 'calcRes' = [ " + ex.Message + " ]");
            }
        }
 public PBNumber(NumberCapacity inWidth)
 {
     this.width = inWidth;
     switch (this.width)
     {
         case NumberCapacity.PB32:
             {
                 this.offset = NumberOffset.PB32;
                 this.exponentLenght = NumberExponentLength.PB32;
                 this.mantissaLenght = NumberMantissaLength.PB32;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB32];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB32];
                 break;
             }
         case NumberCapacity.PB64:
             {
                 this.offset = NumberOffset.PB64;
                 this.exponentLenght = NumberExponentLength.PB64;
                 this.mantissaLenght = NumberMantissaLength.PB64;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB64];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB64];
                 break;
             }
         case NumberCapacity.PB128:
             {
                 this.offset = NumberOffset.PB128;
                 this.exponentLenght = NumberExponentLength.PB128;
                 this.mantissaLenght = NumberMantissaLength.PB128;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB128];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB128];
                 break;
             }
         case NumberCapacity.PB256:
             {
                 this.offset = NumberOffset.PB256;
                 this.exponentLenght = NumberExponentLength.PB256;
                 this.mantissaLenght = NumberMantissaLength.PB256;
                 this.mf = IPBNumber.NumberMFs[(int)NumberMF.PB256];
                 this.cf = IPBNumber.NumberCFs[(int)NumberCF.PB256];
                 break;
             }
         default:
             {
                 break;
             }
     }
     this.name = "Name-" + this.width.ToString() + "[]";
     if (pbConvertion == null)
         pbConvertion = new PBConvertion();
 }
        /// <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;
        }
 /// <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;
 }
        private void tetraMathToolStripMenuItem_Click(object sender, EventArgs e)
        {
            rTBLog.Text += "\n\r _________TETRA MATH TESTING BEGIN";
                String testNumber = "-10000000,1379999e-1055";
                PBNumber pbNumber1 = new PBNumber(testNumber, IPBNumber.NumberCapacity.PB256, IPBNumber.RoundingType.POST_BINARY);
                PBNumber pbNumber2 = new PBNumber(testNumber, IPBNumber.NumberCapacity.PB256, IPBNumber.RoundingType.ZERO);
                PBNumber pbNumber3 = new PBNumber(testNumber, IPBNumber.NumberCapacity.PB256, IPBNumber.RoundingType.NEGATIVE_INFINITY);
                PBNumber pbNumber4 = new PBNumber(testNumber, IPBNumber.NumberCapacity.PB256, IPBNumber.RoundingType.POSITIVE_INFINITY);
                PBNumber pbNumber5 = new PBNumber(testNumber, IPBNumber.NumberCapacity.PB256, IPBNumber.RoundingType.NEAR_INTEGER);

                PBConvertion pbc = new PBConvertion();
                PBMath pbmath = new PBMath();
                pbNumber3 = pbmath.pADD(pbNumber1, pbNumber2);

                String test1 = pbmath.pCMP(pbNumber1, pbNumber1).ToString();
                String test2 = pbmath.pCMP(pbNumber1, pbNumber2).ToString();
                String test3 = pbmath.pCMP(pbNumber2, pbNumber1).ToString();

                rTBLog.Text += "\r\n" + test1;
                rTBLog.Text += "\r\n" + test2;
                rTBLog.Text += "\r\n" + test3;
                rTBLog.Text += "\n\r _________TETRA MATH TESTING END";
        }