internal static string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) { int digits = 0; char fmt = ParseFormatSpecifier(format, out digits); if (fmt == 'x' || fmt == 'X') { return(FormatBigIntegerToHexString(value, fmt, digits, info)); } bool decimalFmt = (fmt == 'g' || fmt == 'G' || fmt == 'd' || fmt == 'D' || fmt == 'r' || fmt == 'R'); if (value._bits == null) { if (fmt == 'g' || fmt == 'G' || fmt == 'r' || fmt == 'R') { if (digits > 0) { format = string.Format(CultureInfo.InvariantCulture, "D{0}", digits.ToString(CultureInfo.InvariantCulture)); } else { format = "D"; } } return(value._sign.ToString(format, info)); } // First convert to base 10^9. const uint kuBase = 1000000000; // 10^9 const int kcchBase = 9; int cuSrc = value._bits.Length; int cuMax; try { cuMax = checked (cuSrc * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } uint[] rguDst = new uint[cuMax]; int cuDst = 0; for (int iuSrc = cuSrc; --iuSrc >= 0;) { uint uCarry = value._bits[iuSrc]; for (int iuDst = 0; iuDst < cuDst; iuDst++) { Debug.Assert(rguDst[iuDst] < kuBase); ulong uuRes = NumericsHelpers.MakeUlong(rguDst[iuDst], uCarry); rguDst[iuDst] = (uint)(uuRes % kuBase); uCarry = (uint)(uuRes / kuBase); } if (uCarry != 0) { rguDst[cuDst++] = uCarry % kuBase; uCarry /= kuBase; if (uCarry != 0) { rguDst[cuDst++] = uCarry; } } } int cchMax; try { // Each uint contributes at most 9 digits to the decimal representation. cchMax = checked (cuDst * kcchBase); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } if (decimalFmt) { if (digits > 0 && digits > cchMax) { cchMax = digits; } if (value._sign < 0) { try { // Leave an extra slot for a minus sign. cchMax = checked (cchMax + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } } } int rgchBufSize; try { // We'll pass the rgch buffer to native code, which is going to treat it like a string of digits, so it needs // to be null terminated. Let's ensure that we can allocate a buffer of that size. rgchBufSize = checked (cchMax + 1); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } char[] rgch = new char[rgchBufSize]; int ichDst = cchMax; for (int iuDst = 0; iuDst < cuDst - 1; iuDst++) { uint uDig = rguDst[iuDst]; Debug.Assert(uDig < kuBase); for (int cch = kcchBase; --cch >= 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } } for (uint uDig = rguDst[cuDst - 1]; uDig != 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } if (!decimalFmt) { // sign = true for negative and false for 0 and positive values bool sign = (value._sign < 0); // The cut-off point to switch (G)eneral from (F)ixed-point to (E)xponential form int precision = 29; int scale = cchMax - ichDst; return(FormatProvider.FormatBigInteger(precision, scale, sign, format, info, rgch, ichDst)); } // Format Round-trip decimal // This format is supported for integral types only. The number is converted to a string of // decimal digits (0-9), prefixed by a minus sign if the number is negative. The precision // specifier indicates the minimum number of digits desired in the resulting string. If required, // the number is padded with zeros to its left to produce the number of digits given by the // precision specifier. int numDigitsPrinted = cchMax - ichDst; while (digits > 0 && digits > numDigitsPrinted) { // pad leading zeros rgch[--ichDst] = '0'; digits--; } if (value._sign < 0) { string negativeSign = info.NegativeSign; for (int i = info.NegativeSign.Length - 1; i > -1; i--) { rgch[--ichDst] = info.NegativeSign[i]; } } return(new string(rgch, ichDst, cchMax - ichDst)); }
private static ReverseStringBuilder CreateBuilder(BigInteger value, NumberFormatInfo info, bool decimalFmt, int digits) { // First convert to base 10^9. const uint NumericBase = 1000000000; // 10^9 const int NumericBaseLog10 = 9; var sourceLength = Length(value.InternalBits); int maxConvertedLength; try { maxConvertedLength = checked (sourceLength * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } var converted = new uint[maxConvertedLength]; var convertedLength = 0; for (var sourceIndex = sourceLength; --sourceIndex >= 0;) { // Take a cipher from the source var carry = value.InternalBits[sourceIndex]; // Add it to converted for (var convertedIndex = 0; convertedIndex < convertedLength; convertedIndex++) { var cipherBlock = NumericsHelpers.MakeUlong(converted[convertedIndex], carry); converted[convertedIndex] = (uint)(cipherBlock % NumericBase); carry = (uint)(cipherBlock / NumericBase); } if (carry != 0) { converted[convertedLength++] = carry % NumericBase; carry /= NumericBase; if (carry != 0) { converted[convertedLength++] = carry; } } } int stringCapacity; try { // Each uint contributes at most 9 digits to the decimal representation. stringCapacity = checked (convertedLength * NumericBaseLog10); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } if (decimalFmt) { if (digits > 0 && stringCapacity < digits) { stringCapacity = digits; } if (value.InternalSign < 0) { try { // Leave an extra slot for a minus sign. stringCapacity = checked (stringCapacity + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException("The value is too large to be represented by this format specifier.", e); } } } var result = new ReverseStringBuilder(stringCapacity); for (var stringIndex = 0; stringIndex < convertedLength - 1; stringIndex++) { var cipherBlock = converted[stringIndex]; for (var cch = NumericBaseLog10; --cch >= 0;) { result.Prepend((char)('0' + cipherBlock % 10)); cipherBlock /= 10; } } for (var cipherBlock = converted[convertedLength - 1]; cipherBlock != 0;) { result.Prepend((char)('0' + cipherBlock % 10)); cipherBlock /= 10; } return(result); }
static String FormatBigInteger(BigInteger value, String format, NumberFormatInfo info) { int digits = 0; char fmt = ParseFormatSpecifier(format, out digits); if (fmt == 'x' || fmt == 'X') { return(FormatBigIntegerToHexString(value, fmt, digits, info)); } bool decimalFmt = (fmt == 'g' || fmt == 'G' || fmt == 'd' || fmt == 'D' || fmt == 'r' || fmt == 'R'); #if SILVERLIGHT || FEATURE_NETCORE if (!decimalFmt) { // Silverlight supports invariant formats only throw new FormatException(SR.GetString("Format_InvalidFormatSpecifier")); } #endif //SILVERLIGHT ||FEATURE_NETCORE if (value._bits == null) { if (fmt == 'g' || fmt == 'G' || fmt == 'r' || fmt == 'R') { if (digits > 0) { format = String.Format(CultureInfo.InvariantCulture, "D{0}", digits.ToString(CultureInfo.InvariantCulture)); } else { format = "D"; } } return(value._sign.ToString(format, info)); } // First convert to base 10^9. const uint kuBase = 1000000000; // 10^9 const int kcchBase = 9; int cuSrc = BigInteger.Length(value._bits); int cuMax; try { cuMax = checked (cuSrc * 10 / 9 + 2); } catch (OverflowException e) { throw new FormatException(SR.GetString("Format_TooLarge"), e); } uint[] rguDst = new uint[cuMax]; int cuDst = 0; for (int iuSrc = cuSrc; --iuSrc >= 0;) { uint uCarry = value._bits[iuSrc]; for (int iuDst = 0; iuDst < cuDst; iuDst++) { Contract.Assert(rguDst[iuDst] < kuBase); ulong uuRes = NumericsHelpers.MakeUlong(rguDst[iuDst], uCarry); rguDst[iuDst] = (uint)(uuRes % kuBase); uCarry = (uint)(uuRes / kuBase); } if (uCarry != 0) { rguDst[cuDst++] = uCarry % kuBase; uCarry /= kuBase; if (uCarry != 0) { rguDst[cuDst++] = uCarry; } } } int cchMax; try { // Each uint contributes at most 9 digits to the decimal representation. cchMax = checked (cuDst * kcchBase); } catch (OverflowException e) { throw new FormatException(SR.GetString("Format_TooLarge"), e); } if (decimalFmt) { if (digits > 0 && digits > cchMax) { cchMax = digits; } if (value._sign < 0) { try { // Leave an extra slot for a minus sign. cchMax = checked (cchMax + info.NegativeSign.Length); } catch (OverflowException e) { throw new FormatException(SR.GetString("Format_TooLarge"), e); } } } int rgchBufSize; try { // We'll pass the rgch buffer to native code, which is going to treat it like a string of digits, so it needs // to be null terminated. Let's ensure that we can allocate a buffer of that size. rgchBufSize = checked (cchMax + 1); } catch (OverflowException e) { throw new FormatException(SR.GetString("Format_TooLarge"), e); } char[] rgch = new char[rgchBufSize]; int ichDst = cchMax; for (int iuDst = 0; iuDst < cuDst - 1; iuDst++) { uint uDig = rguDst[iuDst]; Contract.Assert(uDig < kuBase); for (int cch = kcchBase; --cch >= 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } } for (uint uDig = rguDst[cuDst - 1]; uDig != 0;) { rgch[--ichDst] = (char)('0' + uDig % 10); uDig /= 10; } #if !SILVERLIGHT || FEATURE_NETCORE if (!decimalFmt) { // // Go to the VM for GlobLoc aware formatting // Byte *numberBufferBytes = stackalloc Byte[Number.NumberBuffer.NumberBufferBytes]; Number.NumberBuffer number = new Number.NumberBuffer(numberBufferBytes); // sign = true for negative and false for 0 and positive values number.sign = (value._sign < 0); // the cut-off point to switch (G)eneral from (F)ixed-point to (E)xponential form number.precision = 29; number.digits[0] = '\0'; number.scale = cchMax - ichDst; int maxDigits = Math.Min(ichDst + 50, cchMax); for (int i = ichDst; i < maxDigits; i++) { number.digits[i - ichDst] = rgch[i]; } fixed(char *pinnedExtraDigits = rgch) { return(Number.FormatNumberBuffer(number.PackForNative(), format, info, pinnedExtraDigits + ichDst)); } } #endif //!SILVERLIGHT ||FEATURE_NETCORE // Format Round-trip decimal // This format is supported for integral types only. The number is converted to a string of // decimal digits (0-9), prefixed by a minus sign if the number is negative. The precision // specifier indicates the minimum number of digits desired in the resulting string. If required, // the number is padded with zeros to its left to produce the number of digits given by the // precision specifier. int numDigitsPrinted = cchMax - ichDst; while (digits > 0 && digits > numDigitsPrinted) { // pad leading zeros rgch[--ichDst] = '0'; digits--; } if (value._sign < 0) { String negativeSign = info.NegativeSign; for (int i = info.NegativeSign.Length - 1; i > -1; i--) { rgch[--ichDst] = info.NegativeSign[i]; } } return(new String(rgch, ichDst, cchMax - ichDst)); }
private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo) { regQuo.Set(0); if (regNum._iuLast < regDen._iuLast) { return; } var num1 = regDen._iuLast + 1; var num2 = regNum._iuLast - regDen._iuLast; var num3 = num2; var num4 = regNum._iuLast; while (true) { if (num4 < num2) { num3++; break; } if (regDen._bits[num4 - num2] == regNum._bits[num4]) { num4--; } else { if (regDen._bits[num4 - num2] < regNum._bits[num4]) { num3++; } break; } } if (num3 == 0) { return; } if (fQuo) { regQuo.SetSizeLazy(num3); } var num5 = regDen._bits[num1 - 1]; var num6 = regDen._bits[num1 - 2]; var num7 = NumericsHelpers.CbitHighZero(num5); var num8 = 32 - num7; if (num7 > 0) { num5 = num5 << (num7 & 31) | num6 >> (num8 & 31); num6 = num6 << (num7 & 31); if (num1 > 2) { num6 = num6 | regDen._bits[num1 - 3] >> (num8 & 31); } } regNum.EnsureWritable(); var num9 = num3; while (true) { var num10 = num9 - 1; num9 = num10; if (num10 < 0) { break; } var num = num9 + num1 > regNum._iuLast ? 0 : regNum._bits[num9 + num1]; var num11 = num; var num12 = NumericsHelpers.MakeUlong(num11, regNum._bits[num9 + num1 - 1]); var num13 = regNum._bits[num9 + num1 - 2]; if (num7 > 0) { num12 = num12 << (num7 & 63) | num13 >> (num8 & 31); num13 = num13 << (num7 & 31); if (num9 + num1 >= 3) { num13 = num13 | regNum._bits[num9 + num1 - 3] >> (num8 & 31); } } var num14 = num12 / num5; var num15 = (ulong)((uint)(num12 % num5)); if (num14 > uint.MaxValue) { num15 += num5 * (num14 - uint.MaxValue); num14 = uint.MaxValue; } while (num15 <= uint.MaxValue && num14 * num6 > NumericsHelpers.MakeUlong((uint)num15, num13)) { num14--; num15 += num5; } if (num14 > 0) { var num16 = (ulong)0; for (var i = 0; i < num1; i++) { num16 = num16 + regDen._bits[i] * num14; var num17 = (uint)num16; num16 = num16 >> 32; if (regNum._bits[num9 + i] < num17) { num16 = num16 + 1; } regNum._bits[num9 + i] = regNum._bits[num9 + i] - num17; } if (num11 < num16) { uint num18 = 0; for (var j = 0; j < num1; j++) { num18 = AddCarry(ref regNum._bits[num9 + j], regDen._bits[j], num18); } num14 = num14 - 1; } regNum._iuLast = num9 + num1 - 1; } if (fQuo) { if (num3 != 1) { regQuo._bits[num9] = (uint)num14; } else { regQuo._uSmall = (uint)num14; } } } regNum._iuLast = num1 - 1; regNum.Trim(); }
private static void ModDivCore(ref BigIntegerBuilder regNum, ref BigIntegerBuilder regDen, bool fQuo, ref BigIntegerBuilder regQuo) { regQuo.Set((uint)0); if (regNum._iuLast >= regDen._iuLast) { int num = regDen._iuLast + 1; int num2 = regNum._iuLast - regDen._iuLast; int cu = num2; int index = regNum._iuLast; while (true) { if (index < num2) { cu++; break; } if (regDen._rgu[index - num2] != regNum._rgu[index]) { if (regDen._rgu[index - num2] < regNum._rgu[index]) { cu++; } break; } index--; } if (cu != 0) { if (fQuo) { regQuo.SetSizeLazy(cu); } uint u = regDen._rgu[num - 1]; uint num6 = regDen._rgu[num - 2]; int num7 = NumericsHelpers.CbitHighZero(u); int num8 = 0x20 - num7; if (num7 > 0) { u = (u << num7) | (num6 >> num8); num6 = num6 << num7; if (num > 2) { num6 |= regDen._rgu[num - 3] >> num8; } } regNum.EnsureWritable(); int num9 = cu; while (--num9 >= 0) { uint uHi = ((num9 + num) <= regNum._iuLast) ? regNum._rgu[num9 + num] : 0; ulong num11 = NumericsHelpers.MakeUlong(uHi, regNum._rgu[(num9 + num) - 1]); uint uLo = regNum._rgu[(num9 + num) - 2]; if (num7 > 0) { num11 = (num11 << num7) | (uLo >> num8); uLo = uLo << num7; if ((num9 + num) >= 3) { uLo |= regNum._rgu[(num9 + num) - 3] >> num8; } } ulong num13 = num11 / ((ulong)u); ulong num14 = (uint)(num11 % ((ulong)u)); if (num13 > 0xffffffffL) { num14 += (ulong)u * (num13 - 0xffffffffL); num13 = 0xffffffffL; } while ((num14 <= 0xffffffffL) && ((num13 * num6) > NumericsHelpers.MakeUlong((uint)num14, uLo))) { num13 -= (ulong)1L; num14 += (ulong)u; } if (num13 > 0L) { ulong num15 = 0L; for (int i = 0; i < num; i++) { num15 += regDen._rgu[i] * num13; uint num17 = (uint)num15; num15 = num15 >> 0x20; if (regNum._rgu[num9 + i] < num17) { num15 += (ulong)1L; } regNum._rgu[num9 + i] -= num17; } if (uHi < num15) { uint uCarry = 0; for (int j = 0; j < num; j++) { uCarry = AddCarry(ref regNum._rgu[num9 + j], regDen._rgu[j], uCarry); } num13 -= (ulong)1L; } regNum._iuLast = (num9 + num) - 1; } if (fQuo) { if (cu == 1) { regQuo._uSmall = (uint)num13; } else { regQuo._rgu[num9] = (uint)num13; } } } regNum._iuLast = num - 1; regNum.Trim(); } } }
internal static string FormatBigInteger(BigInteger value, string format, NumberFormatInfo info) { int num3; int num9; int digits = 0; char ch = ParseFormatSpecifier(format, out digits); switch (ch) { case 'x': case 'X': return(FormatBigIntegerToHexString(value, ch, digits, info)); } bool flag = ((((ch == 'g') || (ch == 'G')) || ((ch == 'd') || (ch == 'D'))) || (ch == 'r')) || (ch == 'R'); if (!flag) { throw new FormatException("Format specifier was invalid."); } if (value._bits == null) { switch (ch) { case 'g': case 'G': case 'r': case 'R': if (digits > 0) { format = string.Format(CultureInfo.InvariantCulture, "D{0}", new object[] { digits.ToString(CultureInfo.InvariantCulture) }); } else { format = "D"; } break; } return(value._sign.ToString(format, info)); } int num2 = BigInteger.Length(value._bits); try { num3 = ((num2 * 10) / 9) + 2; } catch (OverflowException exception) { throw new FormatException("The value is too large to be represented by this format specifier.", exception); } uint[] numArray = new uint[num3]; int num4 = 0; int index = num2; while (--index >= 0) { uint uLo = value._bits[index]; for (int k = 0; k < num4; k++) { ulong num8 = NumericsHelpers.MakeUlong(numArray[k], uLo); numArray[k] = (uint)(num8 % ((ulong)0x3b9aca00L)); uLo = (uint)(num8 / ((ulong)0x3b9aca00L)); } if (uLo != 0) { numArray[num4++] = uLo % 0x3b9aca00; uLo /= 0x3b9aca00; if (uLo != 0) { numArray[num4++] = uLo; } } } try { num9 = num4 * 9; } catch (OverflowException exception2) { throw new FormatException("The value is too large to be represented by this format specifier.", exception2); } if (flag) { if ((digits > 0) && (digits > num9)) { num9 = digits; } if (value._sign < 0) { try { num9 += info.NegativeSign.Length; } catch (OverflowException exception3) { throw new FormatException("The value is too large to be represented by this format specifier.", exception3); } } } char[] chArray = new char[num9]; int startIndex = num9; for (int i = 0; i < (num4 - 1); i++) { uint num12 = numArray[i]; int num13 = 9; while (--num13 >= 0) { chArray[--startIndex] = (char)(0x30 + (num12 % 10)); num12 /= 10; } } for (uint j = numArray[num4 - 1]; j != 0; j /= 10) { chArray[--startIndex] = (char)(0x30 + (j % 10)); } int num15 = num9 - startIndex; while ((digits > 0) && (digits > num15)) { chArray[--startIndex] = '0'; digits--; } if (value._sign < 0) { string negativeSign = info.NegativeSign; for (int m = negativeSign.Length - 1; m > -1; m--) { chArray[--startIndex] = negativeSign[m]; } } return(new string(chArray, startIndex, num9 - startIndex)); }