summaryrefslogtreecommitdiff
path: root/Source/Basetypes/BigDec.cs
blob: 6059539b869fe7e942f2595d9a1f60ac3ec278d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Text;
using System.Diagnostics.Contracts;


namespace Microsoft.Basetypes {
  using BIM = System.Numerics.BigInteger;


  /// <summary>
  /// A representation of decimal values.
  /// </summary>
  public struct BigDec {

    // the internal representation
    [Rep]
    internal readonly BIM mantissa;
    [Rep]
    internal readonly int exponent;

    public BIM Mantissa {
      get {
        return mantissa;
      }
    }

    public int Exponent {
      get {
        return exponent;
      }
    }

    public static readonly BigDec ZERO = FromInt(0);
    private static readonly BIM ten = new BIM(10);


    ////////////////////////////////////////////////////////////////////////////
    // Constructors

    [Pure]
    public static BigDec FromInt(int v) {
      return new BigDec(v, 0);
    }

    [Pure]
    public static BigDec FromString(string v) {
      if (v == null) throw new FormatException();

      BIM integral = BIM.Zero;
      BIM fraction = BIM.Zero;
      int exponent = 0;

      int len = v.Length;

      int i = v.IndexOf('e');
      if (i >= 0) {
        if (i + 1 == v.Length) throw new FormatException();
        exponent = Int32.Parse(v.Substring(i + 1, len - i - 1));
        len = i;
      }

      int fractionLen = 0;
      i = v.IndexOf('.');
      if (i >= 0) {
        if (i + 1 == v.Length) throw new FormatException();
        fractionLen = len - i - 1;
        fraction = BIM.Parse(v.Substring(i + 1, fractionLen));
        len = i;
      }

      integral = BIM.Parse(v.Substring(0, len));

      if (!fraction.IsZero) {
        while (fractionLen > 0) {
          integral = integral * ten;
          exponent = exponent - 1;
          fractionLen = fractionLen - 1;
        }
      }

      return new BigDec(integral + fraction, exponent);
    }

    internal BigDec(BIM mantissa, int exponent) {
      if (mantissa.IsZero) {
        this.mantissa = mantissa;
        this.exponent = 0;
      }
      else {
        while (mantissa % ten == BIM.Zero) {
          mantissa = mantissa / ten;
          exponent = exponent + 1;
        }
        this.mantissa = mantissa;
        this.exponent = exponent;
      }
    }


    ////////////////////////////////////////////////////////////////////////////
    // Basic object operations

    [Pure]
    [Reads(ReadsAttribute.Reads.Nothing)]
    public override bool Equals(object obj) {
      if (obj == null)
        return false;
      if (!(obj is BigDec))
        return false;

      return (this == (BigDec)obj);
    }

    [Pure]
    public override int GetHashCode() {
      return this.mantissa.GetHashCode() * 13 + this.exponent.GetHashCode();
    }

    [Pure]
    public override string/*!*/ ToString() {
      Contract.Ensures(Contract.Result<string>() != null);
      return String.Format("{0}e{1}", this.mantissa.ToString(), this.exponent.ToString());
    }


    ////////////////////////////////////////////////////////////////////////////
    // Conversion operations

    [Pure]
    public BIM Floor(BIM? minimum, BIM? maximum) {
      BIM n = this.mantissa;
      
      if (this.exponent >= 0) {
        int e = this.exponent;
        while (e > 0 && (minimum == null || minimum <= n) && (maximum == null || n <= maximum)) {
          n = n * ten;
          e = e - 1;
        }
      }
      else {
        int e = -this.exponent;
        while (e > 0 && !n.IsZero) {
          n = n / ten;
          e = e - 1;
        }
      }

      if (minimum != null && n < minimum)
        return (BIM)minimum;
      else if (maximum != null && maximum < n)
        return (BIM)maximum;
      else
        return n;
    }

    [Pure]
    public String ToDecimalString(int maxDigits) {
      string s = this.mantissa.ToString();
      int digits = (this.mantissa >= 0) ? s.Length : s.Length - 1;
      BIM max = BIM.Pow(10, maxDigits);
      BIM min = -max;

      if (this.exponent >= 0) {
        if (maxDigits < digits || maxDigits - digits < this.exponent) {
          return String.Format("{0}.0", (this.mantissa >= 0) ? max.ToString() : min.ToString());
        }
        else {
          return String.Format("{0}{1}.0", s, new string('0', this.exponent));
        }
      }
      else {
        int exp = -this.exponent;

        if (exp < digits) {
          int intDigits = digits - exp;
          if (maxDigits < intDigits) {
            return String.Format("{0}.0", (this.mantissa >= 0) ? max.ToString() : min.ToString());
          }
          else {
            int fracDigits = Math.Min(maxDigits, digits - intDigits);
            return String.Format("{0}.{1}", s.Substring(0, intDigits), s.Substring(intDigits, fracDigits));
          }
        }
        else {
          int fracDigits = Math.Min(maxDigits, digits);
          return String.Format("0.{0}{1}", new string('0', exp - fracDigits), s.Substring(0, fracDigits));
        }
      }
    }


    ////////////////////////////////////////////////////////////////////////////
    // Basic arithmetic operations

    [Pure]
    public BigDec Abs {
      get {
        return new BigDec(BIM.Abs(this.mantissa), this.exponent);
      }
    }

    [Pure]
    public BigDec Negate {
      get {
        return new BigDec(BIM.Negate(this.mantissa), this.exponent);
      }
    }

    [Pure]
    public static BigDec operator -(BigDec x) {
      return x.Negate;
    }

    [Pure]
    public static BigDec operator +(BigDec x, BigDec y) {
      BIM m1 = x.mantissa;
      int e1 = x.exponent;
      BIM m2 = y.mantissa;
      int e2 = y.exponent;
      if (e2 < e1) {
        m1 = y.mantissa;
        e1 = y.exponent;
        m2 = x.mantissa;
        e2 = x.exponent;
      }

      while (e2 > e1) {
        m2 = m2 * ten;
        e2 = e2 - 1;
      }

      return new BigDec(m1 + m2, e1);
    }

    [Pure]
    public static BigDec operator -(BigDec x, BigDec y) {
      return x + y.Negate;
    }

    [Pure]
    public static BigDec operator *(BigDec x, BigDec y) {
      return new BigDec(x.mantissa * y.mantissa, x.exponent + y.exponent);
    }


    ////////////////////////////////////////////////////////////////////////////
    // Some basic comparison operations

    public bool IsPositive {
      get {
        return (this.mantissa > BIM.Zero);
      }
    }

    public bool IsNegative {
      get {
        return (this.mantissa < BIM.Zero);
      }
    }

    public bool IsZero {
      get {
        return this.mantissa.IsZero;
      }
    }

    [Pure]
    public int CompareTo(BigDec that) {
      if (this.mantissa == that.mantissa && this.exponent == that.exponent) {
        return 0;
      }
      else {
        BigDec d = this - that;
        return d.IsNegative ? -1 : 1;
      }
    }

    [Pure]
    public static bool operator ==(BigDec x, BigDec y) {
      return x.CompareTo(y) == 0;
    }

    [Pure]
    public static bool operator !=(BigDec x, BigDec y) {
      return x.CompareTo(y) != 0;
    }

    [Pure]
    public static bool operator <(BigDec x, BigDec y) {
      return x.CompareTo(y) < 0;
    }

    [Pure]
    public static bool operator >(BigDec x, BigDec y) {
      return x.CompareTo(y) > 0;
    }

    [Pure]
    public static bool operator <=(BigDec x, BigDec y) {
      return x.CompareTo(y) <= 0;
    }

    [Pure]
    public static bool operator >=(BigDec x, BigDec y) {
      return x.CompareTo(y) >= 0;
    }
  }
}