/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/utils/fast_number.vala

  • Committer: Gustav Hartvigsson
  • Date: 2022-06-01 12:30:01 UTC
  • mfrom: (52.1.3 int64)
  • Revision ID: gustav.hartvigsson@gmail.com-20220601123001-b5xs60wwym810hg1
Merged: lp:~gustav.hartvigsson/vqdr/int64

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
  public struct FastNumber {
29
29
    
30
30
    /** Precision used to output values */
31
 
    public const int PRECISION_DIGITS = 2;
 
31
    public const int32 PRECISION_DIGITS = 2;
32
32
    
33
33
    /** Precision factor used to evaluate output */
34
 
    public const int PRECISION_FACTOR = 100;
35
 
    
36
 
    public const int MUL_FACTOR = PRECISION_FACTOR * 10;
37
 
    
38
 
    public long raw_number;
39
 
    
40
 
    public long leading_zeros;
 
34
    public const int32 PRECISION_FACTOR = 100;
 
35
    
 
36
    public const int32 MUL_FACTOR = PRECISION_FACTOR * 10;
 
37
    
 
38
    public int64 raw_number;
 
39
    
 
40
    public int64 leading_zeros;
41
41
    
42
42
    /* XXX
43
43
     * I'm not happy using getters/setters in this struct...
44
44
     * But I tink it'll have to do for simplicity.
45
45
     */
46
 
    public long number {
 
46
    public int64 number {
47
47
      public get {return (this.raw_number / MUL_FACTOR);}
48
48
      public set {
49
49
        this.raw_number = (MUL_FACTOR * value);
62
62
     * 
63
63
     * @param decimal  The decimal part of the number. Defaults to 0.
64
64
     */
65
 
    public FastNumber (long number = 0) {
 
65
    public FastNumber (int64 number = 0) {
66
66
      if (number != 0) {
67
67
        this.raw_number = (number * MUL_FACTOR);
68
68
      } else {
88
88
    /**
89
89
     * Initialises a FastNumber with the internal representation of that number.
90
90
     */
91
 
    public FastNumber.raw (long raw) {
 
91
    public FastNumber.raw (int64 raw) {
92
92
      this.raw_number = raw;
93
93
    }
94
94
    
190
190
    }
191
191
    
192
192
    [CCode (cname = "vqdr_common_fast_number_compare")]
193
 
    public long compare (FastNumber other) {
 
193
    public int64 compare (FastNumber other) {
194
194
      return this.raw_number - other.raw_number;
195
195
    }
196
196
    
201
201
     */
202
202
    public FastNumber round_up () {
203
203
      FastNumber ret;
204
 
      long decimal = raw_number % PRECISION_FACTOR;
 
204
      int64 decimal = raw_number % PRECISION_FACTOR;
205
205
      if (decimal > 0) {
206
206
        ret = FastNumber.raw (raw_number + PRECISION_FACTOR - decimal);
207
207
      } else {
217
217
     */
218
218
    public FastNumber round_down () {
219
219
      FastNumber ret;
220
 
      long decimal = raw_number % PRECISION_FACTOR;
 
220
      int64 decimal = raw_number % PRECISION_FACTOR;
221
221
      if (decimal < 0) {
222
222
        // Is this ever reached?
223
223
        ret = FastNumber.raw (raw_number - PRECISION_FACTOR - decimal);
241
241
        ret_val = (this.raw_number / MUL_FACTOR).to_string ();
242
242
      } else {
243
243
        // Copy stuff so we don't accidentality stomp them.
244
 
        long _raw_number = this.raw_number;
 
244
        int64 _raw_number = this.raw_number;
245
245
        
246
 
        long _integer_part = (_raw_number / MUL_FACTOR);
247
 
        long _decimal_part = (_raw_number - (_integer_part * MUL_FACTOR));
 
246
        int64 _integer_part = (_raw_number / MUL_FACTOR);
 
247
        int64 _decimal_part = (_raw_number - (_integer_part * MUL_FACTOR));
248
248
        
249
249
        var strbldr = new GLib.StringBuilder ();
250
250
        
276
276
      return double.parse (this.to_string (true));
277
277
    }
278
278
    
279
 
    public long to_int () {
 
279
    public int64 to_int () {
280
280
       return (this.raw_number / MUL_FACTOR);
281
281
    }
282
282
    
292
292
    
293
293
    
294
294
    [CCode (cname = "vqdr_common_fast_number_compare")]
295
 
    public static extern long static_compare (FastNumber a, FastNumber b);
 
295
    public static extern int64 static_compare (FastNumber a, FastNumber b);
296
296
    
297
297
    private void parse_raw_number (string str) {
298
298
      //debug (@"(parse_raw_number) str: $str");
299
 
      long ret_val = 0;
 
299
      int64 ret_val = 0;
300
300
      int i_of_dot = str.index_of_char ('.');
301
301
      if (i_of_dot >= 0) {
302
302
        // Get the decimal number from the string, if such a thing exists.
303
303
        if ((str.length - 1 > i_of_dot)) {
304
304
          var intr_str = (str + "000").substring (i_of_dot + 1);
305
305
          // count leading zeros.
 
306
          // (Must be type long, as that is what string.substring () expects.)
306
307
          long i;
307
308
          for (i = 0; intr_str.@get (i) == '0'; i++){}
308
309
          this.leading_zeros = i;
309
310
          // remove leading zeros
310
311
          intr_str = intr_str.substring (i);
311
312
          //debug (@"(parse_raw_number) Intermediate string: $intr_str");
312
 
          ret_val = long.parse (intr_str);
 
313
          ret_val = int64.parse (intr_str);
313
314
        }
314
315
        
315
316
        // debug (@"(parse_raw_number) i_of_dot: $i_of_dot, ret_val (decimal): $ret_val\n");
328
329
        // debug (@"ret_val (normalised): $ret_val\n");
329
330
        
330
331
        // get integer number
331
 
        ret_val = ret_val + (long.parse (str.substring (0, i_of_dot))
 
332
        ret_val = ret_val + (int64.parse (str.substring (0, i_of_dot))
332
333
                            * MUL_FACTOR);
333
334
        
334
335
      } else {
335
 
        ret_val = (long.parse (str) * MUL_FACTOR);
 
336
        ret_val = (int64.parse (str) * MUL_FACTOR);
336
337
      }
337
338
      //debug (@"(parse_raw_number) ret_val (finished): $ret_val\n");
338
339
      this.raw_number = ret_val;