/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/libvee/fast_number.vala

  • Committer: Gustav Hartvigsson
  • Date: 2024-12-22 00:30:29 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241222003029-k74ogrm32zobz325
[General] Split libvee into it's own library.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
namespace VQDR.Common {
 
1
/*
 
2
 * The contects of this file is in the Public Domain.
 
3
 *
 
4
 * Created by Gustav Hartivgsson.
 
5
 */
 
6
using GLib;
 
7
 
 
8
namespace Vee {
2
9
  
3
10
  /**
4
 
   * Fast Numbers are a decimal representanion of numbers in the folm of 
5
 
   * a normal integer value. All internal nummbers are multiples of 1000, so the
6
 
   * there is room for two three decimal ponts.
7
 
   * 
8
 
   * Math done on these numbers are done using standard integer operations, and
9
 
   * not floating point math.
 
11
   * Fast Numbers are a decimal representation of numbers in the form of
 
12
   * a normal integer value. All internal numbers are multiples of 1000, so the
 
13
   * there is room for two three decimal points.
 
14
   * 
 
15
   * Maths done on these numbers are done using standard integer operations, and
 
16
   * not floating point maths.
 
17
   * 
 
18
   * The decimal part of the FastNumber has a maximum of 3 decimals.
 
19
   * 
 
20
   * How the value is divided internally is as follows:
 
21
   * {{{
 
22
   *  (base 10) [0 0 0 0 0 .... 0 | 0 0 0  ]
 
23
   *            [non-decial part  | decimal]
 
24
   * }}}
 
25
   *
10
26
   */
11
 
  class FastNumber {
12
 
    public const long MUL_FACTOR = 1000;
13
 
    
14
 
    public long raw_number { public get; private set; }
15
 
    
16
 
    public long number {
17
 
      get {return raw_number / MUL_FACTOR;}
18
 
      set {raw_number = number * MUL_FACTOR;}
19
 
    }
20
 
    
21
 
    public FastNumber (long val = 0) {
22
 
      this.number = val;
23
 
    }
 
27
  
 
28
  public errordomain MathError {
 
29
    DIVIDE_BY_ZERO;
 
30
  }
 
31
  
 
32
  public struct FastNumber {
 
33
    
 
34
    
 
35
    /** Precision used to output values */
 
36
    public const int32 PRECISION_DIGITS = 2;
 
37
    
 
38
    /** Precision factor used to evaluate output */
 
39
    public const int32 PRECISION_FACTOR = 100;
 
40
    
 
41
    public const int32 MUL_FACTOR = PRECISION_FACTOR * 10;
 
42
    
 
43
    public int64 raw_number;
 
44
    
 
45
    public int64 leading_zeros;
 
46
    
 
47
    /* XXX
 
48
     * I'm not happy using getters/setters in this struct...
 
49
     * But I tink it'll have to do for simplicity.
 
50
     */
 
51
    public int64 number {
 
52
      public get {return (this.raw_number / MUL_FACTOR);}
 
53
      public set {
 
54
        this.raw_number = (MUL_FACTOR * value);
 
55
      }
 
56
    }
 
57
    
 
58
    /**
 
59
     * Initialises a FastNumber.
 
60
     *
 
61
     * Note: Due to implementation details, you can't pass a decimal part that
 
62
     *       is less than .1, as we are normalising decimal values to the
 
63
     *       correct place.
 
64
     * 
 
65
     * @param number   The number that are to be set as the none-decimal part of
 
66
     *                 the number. Defaults to 0.
 
67
     * 
 
68
     * @param decimal  The decimal part of the number. Defaults to 0.
 
69
     */
 
70
    
 
71
    public FastNumber (int64 number = 0) {
 
72
      if (number != 0) {
 
73
        this.raw_number = (number * MUL_FACTOR);
 
74
      } else {
 
75
        this.raw_number = 0;
 
76
      }
 
77
    }
 
78
    
 
79
    /**
 
80
     * Do a deep copy of a FastNumber.
 
81
     */
24
82
    
25
83
    public FastNumber.copy (FastNumber other) {
26
84
      this.raw_number = other.raw_number;
27
85
    }
28
86
    
 
87
    /**
 
88
     * Initialises a FastNumber from a string.
 
89
     * 
 
90
     * Can be a decimal representation.
 
91
     */
 
92
    
29
93
    public FastNumber.from_string (string str) {
30
 
      this.raw_number = parse_raw_number (str);
31
 
    }
32
 
    
33
 
    private static long parse_raw_number (string str) {
34
 
      long ret_val = 0;
 
94
      parse_raw_number (str);
 
95
    }
 
96
    /**
 
97
     * Initialises a FastNumber with the internal representation of that number.
 
98
     */
 
99
    
 
100
    public FastNumber.raw (int64 raw) {
 
101
      this.raw_number = raw;
 
102
    }
 
103
    
 
104
    
 
105
    public FastNumber.from_float (double float_number) {
 
106
      // XXX Do we need a faster way of doing this?
 
107
      parse_raw_number (float_number.to_string ()); 
 
108
    }
 
109
    
 
110
    /**
 
111
     * Add this to an other FastNumber.
 
112
     * 
 
113
     * {{{
 
114
     * var f1 = FastNumber (3);   // f1 = 3
 
115
     * var f2 = FastNumber (2);   // f2 = 2
 
116
     * var f3 = f1.add (f2);      // f3 = 5
 
117
     * }}}
 
118
     * 
 
119
     * @return a newly initialised FastNumber.
 
120
     * 
 
121
     * @param other The other fast number you want to add to this value.
 
122
     */
 
123
    
 
124
    public FastNumber add (FastNumber? other) {
 
125
      if (other == null) {
 
126
        return  FastNumber.copy (this);
 
127
      }
 
128
      
 
129
      var v = FastNumber ();
 
130
      v.raw_number = (this.raw_number + other.raw_number);
 
131
      return v;
 
132
    }
 
133
    
 
134
    /**
 
135
     * Add this to an other FastNumber.
 
136
     * 
 
137
     * {{{
 
138
     * var f1 = FastNumber (3);   // f1 = 3
 
139
     * var f2 = FastNumber (2);   // f2 = 2
 
140
     * var f3 = f1.subtract (f2); // f3 = 1
 
141
     * }}}
 
142
     * 
 
143
     * @return a newly initialised FastNumber.
 
144
     * 
 
145
     * @param other  The other fast number you want to subtract from this 
 
146
     *               FastNumber.
 
147
     */
 
148
    
 
149
    public FastNumber subtract (FastNumber? other) {
 
150
      if (other == null) {
 
151
        return  FastNumber.copy (this);
 
152
      }
 
153
      
 
154
      var v = FastNumber ();
 
155
      v.raw_number = (this.raw_number - other.raw_number);
 
156
      return v;
 
157
    }
 
158
    
 
159
    /**
 
160
     * Multiply this FastNumber with another FastNumber.
 
161
     * 
 
162
     * {{{
 
163
     * var f1 = FastNumber (3);   // f1 = 3
 
164
     * var f2 = FastNumber (2);   // f2 = 2
 
165
     * var f3 = f1.multiply (f2); // f3 = 6
 
166
     * }}}
 
167
     * 
 
168
     * @return a newly initialised FastNumber.
 
169
     * 
 
170
     * @param other The value you want to multiply this value with.
 
171
     */
 
172
    
 
173
    public FastNumber multiply (FastNumber? other) {
 
174
      if (other == null || other.raw_number == 0 || this.raw_number == 0) {
 
175
        return  FastNumber ();
 
176
      }
 
177
      
 
178
      var ret = FastNumber ();
 
179
      ret.raw_number = ((this.raw_number * other.raw_number) / MUL_FACTOR);
 
180
      return ret;
 
181
    }
 
182
    
 
183
    /**
 
184
     * Divide this FastNumbers with another FastNumber.
 
185
     * 
 
186
     * {{{
 
187
     * var f1 = FastNumber (6);   // f1 = 6
 
188
     * var f2 = FastNumber (2);   // f2 = 2
 
189
     * var f3 = f1.multiply (f2); // f3 = 3
 
190
     * }}}
 
191
     * 
 
192
     * @return a newly initialised FastNumber.
 
193
     * 
 
194
     * @param other The value you want to multiply this value with.
 
195
     */
 
196
    
 
197
    public FastNumber divide (FastNumber other) throws MathError {
 
198
      if (other.raw_number == 0) {
 
199
        throw new MathError.DIVIDE_BY_ZERO ("trying to divide by zero");
 
200
      }
 
201
      var ret =  FastNumber ();
 
202
      ret.raw_number = ((this.raw_number * MUL_FACTOR) / other.raw_number);
 
203
      return ret;
 
204
    }
 
205
    
 
206
    
 
207
    public int64 compare (FastNumber other) {
 
208
      return this.raw_number - other.raw_number;
 
209
    }
 
210
    
 
211
    /**
 
212
     * Round up this FastNumber and returns a new FastNumber.
 
213
     * 
 
214
     * @return a rounded up FastNumber.
 
215
     */
 
216
    
 
217
    public FastNumber round_up () {
 
218
      FastNumber ret;
 
219
      int64 decimal = raw_number % PRECISION_FACTOR;
 
220
      if (decimal > 0) {
 
221
        ret = FastNumber.raw (raw_number + PRECISION_FACTOR - decimal);
 
222
      } else {
 
223
        ret = FastNumber.raw (raw_number - decimal);
 
224
      }
 
225
      return ret;
 
226
    }
 
227
    
 
228
    /**
 
229
     * Round up this FastNumber and returns a new FastNumber.
 
230
     * 
 
231
     * @return a rounded down FastNumber.
 
232
     */
 
233
    public FastNumber round_down () {
 
234
      FastNumber ret;
 
235
      int64 decimal = raw_number % PRECISION_FACTOR;
 
236
      if (decimal < 0) {
 
237
        // Is this ever reached?
 
238
        ret = FastNumber.raw (raw_number - PRECISION_FACTOR - decimal);
 
239
      } else {
 
240
        ret = FastNumber.raw (raw_number - decimal);
 
241
      }
 
242
      return ret;
 
243
    }
 
244
    
 
245
    /**
 
246
     * FastNumber to string.
 
247
     * 
 
248
     * @return a string
 
249
     * 
 
250
     * @param decimal whether to return the decimal portion of the number in 
 
251
     *                the string. Default = false.
 
252
     */
 
253
    
 
254
    public string to_string (bool decimal = false) {
 
255
      string ret_val = null;
 
256
      if (!decimal) {
 
257
        ret_val = (this.raw_number / MUL_FACTOR).to_string ();
 
258
      } else {
 
259
        // Copy stuff so we don't accidentality stomp them.
 
260
        int64 _raw_number = this.raw_number;
 
261
        
 
262
        int64 _integer_part = (_raw_number / MUL_FACTOR);
 
263
        int64 _decimal_part = (_raw_number - (_integer_part * MUL_FACTOR));
 
264
        
 
265
        var strbldr = new GLib.StringBuilder ();
 
266
        
 
267
        // normalise the decimal part.
 
268
        // (XXX This is rather expensive, is there a better way of doing this?).
 
269
        if (_decimal_part != 0) {
 
270
          while ((_decimal_part % 10) == 0) {
 
271
            _decimal_part = _decimal_part / 10;
 
272
          }
 
273
        }
 
274
        
 
275
        strbldr.append (_integer_part.to_string ())
 
276
               .append_c ('.');
 
277
        
 
278
        
 
279
        for ( var i = this.leading_zeros ; i > 0 ; i--) {
 
280
          strbldr.append_c ('0');
 
281
        }
 
282
        
 
283
        strbldr.append (_decimal_part.to_string ());
 
284
        
 
285
        ret_val = strbldr.str;
 
286
      }
 
287
      return ret_val;
 
288
    }
 
289
    
 
290
    
 
291
    public double to_float () {
 
292
      // XXX This probobly needs to something faster?
 
293
      return double.parse (this.to_string (true));
 
294
    }
 
295
    
 
296
    
 
297
    public int64 to_int () {
 
298
       return (this.raw_number / MUL_FACTOR);
 
299
    }
 
300
    
 
301
    /**
 
302
     * Check if two FastNumbers are equal.
 
303
     * 
 
304
     * @return true if this is equal to the other.
 
305
     * @return false if this is not equal to the other.
 
306
     */
 
307
    public bool equals (FastNumber other) {
 
308
      return (this.raw_number == other.raw_number);
 
309
    }
 
310
    
 
311
    
 
312
    
 
313
    public static extern int64 static_compare (FastNumber a, FastNumber b);
 
314
    
 
315
    
 
316
    private void parse_raw_number (string str) {
 
317
      //debug (@"(parse_raw_number) str: $str");
 
318
      int64 ret_val = 0;
35
319
      int i_of_dot = str.index_of_char ('.');
36
320
      if (i_of_dot >= 0) {
37
 
      
38
321
        // Get the decimal number from the string, if such a thing exists.
39
322
        if ((str.length - 1 > i_of_dot)) {
40
 
          ret_val = long.parse ((str + "000").substring (i_of_dot + 1));
 
323
          var intr_str = (str + "000").substring (i_of_dot + 1);
 
324
          // count leading zeros.
 
325
          // (Must be type long, as that is what string.substring () expects.)
 
326
          long i;
 
327
          for (i = 0; intr_str.@get (i) == '0'; i++){}
 
328
          this.leading_zeros = i;
 
329
          // remove leading zeros
 
330
          intr_str = intr_str.substring (i);
 
331
          //debug (@"(parse_raw_number) Intermediate string: $intr_str");
 
332
          ret_val = int64.parse (intr_str);
41
333
        }
42
334
        
 
335
        // debug (@"(parse_raw_number) i_of_dot: $i_of_dot, ret_val (decimal): $ret_val\n");
 
336
        
43
337
        // Normalise the digits.
44
338
        while (ret_val > MUL_FACTOR) {
45
339
          ret_val = ret_val / 10;
46
 
        }
47
 
        
48
 
        // Add intiger number
49
 
        ret_val = ret_val + (long.parse ("0" + str.substring (0, i_of_dot))
 
340
          // debug (@"(parse_raw_number) retval (loop): $ret_val");
 
341
        }
 
342
        
 
343
        for (var i = leading_zeros; i > 0; i--) {
 
344
          ret_val = ret_val / 10;
 
345
          // debug (@"(parse_raw_number) retval (loop2): $ret_val");
 
346
        }
 
347
        
 
348
        // debug (@"ret_val (normalised): $ret_val\n");
 
349
        
 
350
        // get integer number
 
351
        ret_val = ret_val + (int64.parse (str.substring (0, i_of_dot))
50
352
                            * MUL_FACTOR);
 
353
        
51
354
      } else {
52
 
        ret_val = long.parse (str) * MUL_FACTOR;
53
 
      }
54
 
      return ret_val;
55
 
    }
56
 
    
57
 
    
58
 
    public FastNumber add (FastNumber? other) {
59
 
      if (other == null) {
60
 
        return new FastNumber.copy (this);
61
 
      }
62
 
      
63
 
      var v = new FastNumber (this.raw_number + other.raw_number);
64
 
      
65
 
      return v;
66
 
    }
67
 
    
68
 
    public FastNumber subtract (FastNumber? other) {
69
 
      if (other == null) {
70
 
        return new FastNumber.copy (this);
71
 
      }
72
 
      
73
 
      var v = new FastNumber (this.raw_number - other.raw_number);
74
 
      
75
 
      return v;
76
 
    }
77
 
    
78
 
    public FastNumber multiply (FastNumber? other) {
79
 
      if (other == null || other.raw_value == 0) {
80
 
        return new FastNumber ();
81
 
      }
82
 
      
83
 
      return new FastNumber ((this.raw_number * other.raw_number) / MUL_FACTOR);
84
 
    }
85
 
    
86
 
    public FastNumber divide (FastNumber? other) throws MathError {
87
 
      if (other.raw_number == 0) {
88
 
        throw new MathError.DIVIDE_BY_ZERO
89
 
                                      ("FantNumber - trying to divide by zero");
90
 
      }
91
 
      
92
 
      return new FastNumber ((this.raw_number * MUL_FACTOR) / other.raw_number);
93
 
    }
94
 
    
 
355
        ret_val = (int64.parse (str) * MUL_FACTOR);
 
356
      }
 
357
      //debug (@"(parse_raw_number) ret_val (finished): $ret_val\n");
 
358
      this.raw_number = ret_val;
 
359
    }
95
360
  }
96
 
  
97
361
}
 
362