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