1 --- ./Runtime/Microsoft.Dynamic/Math/BigIntegerV2.cs 2009-10-19 13:36:38.000000000 -0400
2 +++ /cvs/mcs/class/System.Numerics/System.Numerics/BigInteger.cs 2009-10-31 12:13:55.000000000 -0400
6 * ***************************************************************************/
10 using System.Collections.Generic;
12 using System.Diagnostics.CodeAnalysis;
13 using System.Globalization;
15 -using Microsoft.Contracts;
16 -using Microsoft.Scripting.Utils;
18 namespace Microsoft.Scripting.Math {
21 /// (inverse of ToByteArray())
23 public static BigInteger Create(byte[] v) {
24 - ContractUtils.RequiresNotNull(v, "v");
26 + throw new ArgumentNullException ("v");
27 if (v.Length == 0) return Create(0);
29 int byteCount = v.Length;
33 public BigInteger(int sign, params uint[] data) {
34 - ContractUtils.RequiresNotNull(data, "data");
35 - ContractUtils.Requires(sign >= -1 && sign <= +1, "sign");
37 + throw new ArgumentNullException ("data");
38 + if (!(sign >= -1 && sign <= +1))
39 + throw new ArgumentException ("sign");
40 int length = GetLength(data);
41 - ContractUtils.Requires(length == 0 || sign != 0, "sign");
42 + if (!(length == 0 || sign != 0))
43 + throw new ArgumentException ("sign");
46 this.sign = (short)(length == 0 ? 0 : sign);
50 public bool TryToFloat64(out double result) {
51 - return StringUtils.TryParseDouble(ToString(10),
52 + return double.TryParse(ToString(10),
53 System.Globalization.NumberStyles.Number,
54 System.Globalization.CultureInfo.InvariantCulture.NumberFormat,
56 @@ -1355,17 +1356,92 @@
61 public override string ToString() {
66 - public string ToString(int radix) {
67 - return MathUtils.BigIntegerToString(copy(data), sign, radix);
68 + // generated by scripts/radix_generator.py
69 + private static readonly uint[] maxCharsPerDigit = { 0, 0, 31, 20, 15, 13, 12, 11, 10, 10, 9, 9, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 };
70 + private static readonly uint[] groupRadixValues = { 0, 0, 2147483648, 3486784401, 1073741824, 1220703125, 2176782336, 1977326743, 1073741824, 3486784401, 1000000000, 2357947691, 429981696, 815730721, 1475789056, 2562890625, 268435456, 410338673, 612220032, 893871739, 1280000000, 1801088541, 2494357888, 3404825447, 191102976, 244140625, 308915776, 387420489, 481890304, 594823321, 729000000, 887503681, 1073741824, 1291467969, 1544804416, 1838265625, 2176782336 };
72 + public static ArgumentOutOfRangeException MakeArgumentOutOfRangeException(string paramName, object actualValue, string message) {
73 + return new ArgumentOutOfRangeException(paramName, string.Format("{0} (actual value is '{1}')", message, actualValue));
76 + internal static string BigIntegerToString(uint[] d, int sign, int radix) {
78 + throw MakeArgumentOutOfRangeException("radix", radix, "radix must be >= 2");
81 + throw MakeArgumentOutOfRangeException("radix", radix, "radix must be <= 36");
89 + List<uint> digitGroups = new List<uint>();
91 + uint groupRadix = groupRadixValues[radix];
93 + uint rem = div(d, ref dl, groupRadix);
94 + digitGroups.Add(rem);
97 + StringBuilder ret = new StringBuilder();
102 + int digitIndex = digitGroups.Count - 1;
104 + char[] tmpDigits = new char[maxCharsPerDigit[radix]];
106 + AppendRadix((uint)digitGroups[digitIndex--], (uint)radix, tmpDigits, ret, false);
107 + while (digitIndex >= 0) {
108 + AppendRadix((uint)digitGroups[digitIndex--], (uint)radix, tmpDigits, ret, true);
110 + return ret.Length == 0 ? "0" : ret.ToString();
113 + private static uint div(uint[] n, ref int nl, uint d) {
116 + bool seenNonZero = false;
118 + rem <<= BitsPerDigit;
120 + uint v = (uint)(rem / d);
123 + if (!seenNonZero) nl--;
125 + seenNonZero = true;
132 + private static void AppendRadix(uint rem, uint radix, char[] tmp, StringBuilder buf, bool leadingZeros) {
133 + const string symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
135 + int digits = tmp.Length;
137 + while (i > 0 && (leadingZeros || rem != 0)) {
138 + uint digit = rem % radix;
140 + tmp[--i] = symbols[(int)digit];
142 + if (leadingZeros) buf.Append(tmp);
143 + else buf.Append(tmp, i, digits - i);
146 + public string ToString(int radix) {
147 + return BigIntegerToString(copy(data), sign, radix);
151 public override int GetHashCode() {
152 // The Object.GetHashCode function needs to be consistent with the Object.Equals function.
153 // Languages that build on top of this may have a more flexible equality function and
154 @@ -1395,12 +1471,10 @@
159 public override bool Equals(object obj) {
160 return Equals(obj as BigInteger);
164 public bool Equals(BigInteger other) {
165 if (object.ReferenceEquals(other, null)) return false;
166 return this == other;
167 @@ -1492,17 +1566,14 @@
169 #region IConvertible Members
172 public TypeCode GetTypeCode() {
173 return TypeCode.Object;
177 public bool ToBoolean(IFormatProvider provider) {
182 public byte ToByte(IFormatProvider provider) {
184 if (AsUInt32(out ret) && (ret & ~0xFF) == 0) {
185 @@ -1561,7 +1632,6 @@
190 public char ToChar(IFormatProvider provider) {
192 if (AsInt32(out ret) && (ret <= Char.MaxValue) && (ret >= Char.MinValue)) {
193 @@ -1570,24 +1640,20 @@
194 throw new OverflowException("big integer won't fit into char");
198 public DateTime ToDateTime(IFormatProvider provider) {
199 throw new NotImplementedException();
203 public decimal ToDecimal(IFormatProvider provider) {
205 if (AsDecimal(out ret)) return ret;
206 throw new OverflowException("big integer won't fit into decimal");
210 public double ToDouble(IFormatProvider provider) {
215 public short ToInt16(IFormatProvider provider) {
217 if (AsInt32(out ret) && (ret <= short.MaxValue) && (ret >= short.MinValue)) {
218 @@ -1596,7 +1662,6 @@
219 throw new OverflowException("big integer won't fit into short");
223 public int ToInt32(IFormatProvider provider) {
225 if (AsInt32(out ret)) {
226 @@ -1605,7 +1670,6 @@
227 throw new OverflowException("big integer won't fit into int");
231 public long ToInt64(IFormatProvider provider) {
233 if (AsInt64(out ret)) {
234 @@ -1614,7 +1678,7 @@
235 throw new OverflowException("big integer won't fit into long");
238 - [CLSCompliant(false), Confined]
239 + [CLSCompliant(false)]
240 public sbyte ToSByte(IFormatProvider provider) {
242 if (AsInt32(out ret) && (ret <= sbyte.MaxValue) && (ret >= sbyte.MinValue)) {
243 @@ -1623,17 +1687,14 @@
244 throw new OverflowException("big integer won't fit into sbyte");
248 public float ToSingle(IFormatProvider provider) {
249 return checked((float)ToDouble(provider));
253 public string ToString(IFormatProvider provider) {
258 public object ToType(Type conversionType, IFormatProvider provider) {
259 if (conversionType == typeof(BigInteger)) {
261 @@ -1641,7 +1702,7 @@
262 throw new NotImplementedException();
265 - [CLSCompliant(false), Confined]
266 + [CLSCompliant(false)]
267 public ushort ToUInt16(IFormatProvider provider) {
269 if (AsUInt32(out ret) && ret <= ushort.MaxValue) {
270 @@ -1650,7 +1711,7 @@
271 throw new OverflowException("big integer won't fit into ushort");
274 - [CLSCompliant(false), Confined]
275 + [CLSCompliant(false)]
276 public uint ToUInt32(IFormatProvider provider) {
278 if (AsUInt32(out ret)) {
279 @@ -1659,7 +1720,7 @@
280 throw new OverflowException("big integer won't fit into uint");
283 - [CLSCompliant(false), Confined]
284 + [CLSCompliant(false)]
285 public ulong ToUInt64(IFormatProvider provider) {
287 if (AsUInt64(out ret)) {
288 @@ -1724,4 +1785,3 @@