//, PBNumber inObjectNumber /// <summary> /// Calculates Exponent for specified number /// Uses : Number.BinaryIntPart,Number.BinaryFloatPart /// </summary> /// <param name="inNumber">Number - var from which exponenta need to be taken</param> /// <param name="Left_Right">False - Left part og number, else - Right </param> /// <returns>Returns Exponent in 2cc</returns> private void selectExp(IFPartsOfNumber inStingNumber, IPBNumber.NumberCapacity inCapacity, IPBNumber.NumberFormat inNumberFormat) { int z = 0; int Offset = 0; String temp, result = ""; String bynaryStringInt = "", bynaryStringFloat = ""; bynaryStringInt = inStingNumber.IntegerPart; bynaryStringFloat = inStingNumber.FloatPart; try { Offset = (int)pbConvertion.getNumberOffset(inCapacity); if (bynaryStringInt.IndexOf('1') != -1) { temp = bynaryStringInt; temp = temp.TrimStart('0'); z = temp.Length - (temp.IndexOf('1') + 1); if (z > Offset) z = Offset; } else if (bynaryStringFloat.IndexOf('1') != -1) { temp = bynaryStringFloat; temp = temp.TrimEnd('0'); z = (temp.IndexOf('1') + 1); z *= -1; if (z < -Offset) z = -Offset; } else { z = -Offset; } result = pbConvertion.convert10to2IPart((z + Offset).ToString()); this.Exponent = result; } catch (Exception ex) { throw new PBFunctionException("Exception in Func ['selectExp'] Mess=[" + ex.Message + "]"); } }
// PBNumber inObjectNumber, /// <summary> /// Calculates Mantissa for specified number. Define Exponent before Mantissa for correct running algorithm. /// Uses funcs: isStringZero,convert10to2IPart, checkStringFull, sumExp /// Uses Vars: Number.BinaryIntPart,Number.BinaryFloatPart /// </summary> /// <param name="inNumber">Number - var from which mantissa need to be taken</param> /// <param name="Left_Right">False - Left part og number, else - Right </param> /// <returns>Returns Mantissa in 2cc</returns> private void selectMantissa(IFPartsOfNumber inStringNumber, IPBNumber.NumberCapacity inCapacity, IPBNumber.NumberFormat inNumberFormat, IPBNumber.RoundingType inRoundingType) { int i, l = 0; int currMBits; String result = ""; String[] tempArray; int offsetDot = 1; String bynaryStringInt = "", bynaryStringFloat = ""; if (inNumberFormat == 0) { bynaryStringInt = inStringNumber.IntegerPart; bynaryStringFloat = inStringNumber.FloatPart; } try { if ((bynaryStringInt != null) && (bynaryStringFloat != null)) { if ((bynaryStringInt != "") && (bynaryStringFloat != "")) { if (bynaryStringInt.IndexOf('1') != -1) { offsetDot = bynaryStringInt.IndexOf('1'); result = bynaryStringInt.Substring(offsetDot + 1) + bynaryStringFloat; } else if (bynaryStringFloat.IndexOf('1') != -1) if (pbConvertion.isStringZero(Exponent)) result = "" + bynaryStringFloat.Substring((int)pbConvertion.getNumberOffset(inCapacity) - 1, (int)pbConvertion.getNumberMantissaLength(inCapacity) + 1); else { offsetDot = bynaryStringFloat.IndexOf('1') + 1; result = "" + bynaryStringFloat.Substring(offsetDot); } else { currMBits = (int)pbConvertion.getNumberMantissaLength(inCapacity); tempArray = new String[currMBits]; for (i = 0; i < currMBits; i++) tempArray[i] = "0"; result = result + String.Join("", tempArray); } } else { throw new PBArithmeticException("Exception in Func ['selectMantissa'] Mess=[ Empty String - BynaryIntPart or BynaryFloatPart ] ( PB" + inCapacity + "=" + inStringNumber.ToString() + ")"); } } else { throw new PBArithmeticException("Exception in Func ['selectMantissa'] Mess=[ Null - BynaryIntPart or BynaryFloatPart ] ( PB" + inCapacity + "=" + inStringNumber.ToString() + ")"); } currMBits = (int)pbConvertion.getNumberMantissaLength(inCapacity); if (result.Length <= (int)currMBits) { // After Research Modification HERE NEEDED ! l = currMBits + 1 - result.Length; tempArray = new String[l]; for (i = 0; i < l; i++) { tempArray[i] = "0"; } result = result + String.Join("", tempArray); } Round(inRoundingType, result, currMBits, inStringNumber, offsetDot, inCapacity); } catch (Exception ex) { throw new PBFunctionException("Exception in Func ['selectMantissa'] Mess=[" + ex.Message + "]"); } }
public void Round(IPBNumber.RoundingType inRoundingType, String result, int currMBits, IFPartsOfNumber inStringNumber, int offsetDot, IPBNumber.NumberCapacity inCapacity) { String M = ""; switch (inRoundingType) { case IPBNumber.RoundingType.ZERO: // to ZERO M = result.Substring(0, currMBits); break; case IPBNumber.RoundingType.NEAR_INTEGER:// to INTEGER if (pbConvertion.isStringZero(Exponent)) { String[] tempArray = new String[offsetDot]; for (int i = 0; i < offsetDot; i++) tempArray[i] = "0"; M = M + String.Join("", tempArray); M += result.Substring(0, currMBits + 1 - offsetDot); } else M = result.Substring(0, currMBits + 0); if ((result[currMBits] == '1') && (inStringNumber.Sign[0] == '+')) { if (!pbConvertion.checkStringFull(M)) { M = pbConvertion.convert2to10IPart(M); M = pbConvertion.Addition(M, "1"); } else { M = "0"; if (!pbConvertion.checkStringFull(Exponent)) { pbConvertion.sumExp(Exponent, "1", inCapacity); } } M = pbConvertion.convert10to2IPart(M); if (M.Length + 1 == currMBits) { M = "0" + M; } else if (M.Length < currMBits) { int l = currMBits - M.Length; String[] tempArray = new String[l]; for (int i = 0; i < l; i++) tempArray[i] = "0"; M = String.Join("", tempArray) + M; } } break; case IPBNumber.RoundingType.POSITIVE_INFINITY:// +Inf M = result.Substring(0, currMBits); if (inStringNumber.Sign[0] == '0') { if (!pbConvertion.checkStringFull(M)) { M = pbConvertion.convert2to10IPart(M); M = pbConvertion.Addition(M, "1"); } else { M = "0"; if (!pbConvertion.checkStringFull(Exponent)) { pbConvertion.sumExp(Exponent, "1", inCapacity); } } M = pbConvertion.convert10to2IPart(M); } break; case IPBNumber.RoundingType.NEGATIVE_INFINITY: // -Inf M = result.Substring(0, currMBits); if (inStringNumber.Sign[0] == '1') { if (!pbConvertion.checkStringFull(M)) { M = pbConvertion.convert2to10IPart(M); M = pbConvertion.Addition(M, "1"); } else { M = "0"; if (!pbConvertion.checkStringFull(Exponent)) { pbConvertion.sumExp(Exponent, "1", inCapacity); } } M = pbConvertion.convert10to2IPart(M); } break; case IPBNumber.RoundingType.POST_BINARY: if (result.Length >= currMBits) { M = result.Substring(0, currMBits); } else { M = result; while (M.Length < currMBits) { M = "0" + M; } } if (result.Length >= currMBits + 2) { String nonSignificantBits = result.Substring(currMBits, 2); if ((nonSignificantBits == "01") || (nonSignificantBits == "10")) { int lastZero = M.LastIndexOf('0'); if (lastZero == -1) { if (nonSignificantBits == "10") { M = pbConvertion.getEmptyMantissa(inCapacity).ToString(); //TODO: Create function for addition in binary, and delete next 3th lines of code String tempExponent = pbConvertion.convert2to10IPart(this.Exponent);// tempExponent = pbConvertion.Addition(tempExponent, "1"); // ADD 1 to Exponent this.Exponent = pbConvertion.convert10to2IPart(tempExponent); // } } else { //TODO: Control here M = M.Substring(0, lastZero) + "M"; while (M.Length < currMBits) { M += "A"; } } }else if (nonSignificantBits == "11") { if (M.LastIndexOf('0')!=-1) { M = pbConvertion.convert10to2IPart( pbConvertion.Addition(pbConvertion.convert2to10IPart(M), "1") ); }else { Round(IPBNumber.RoundingType.NEAR_INTEGER, result, currMBits, inStringNumber, offsetDot, inCapacity); } } } break; } this.Mantissa = M; }