/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/common/fast-number.vala

  • Committer: Gustav Hartvigsson
  • Date: 2021-05-24 15:49:06 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210524154906-z65s7fxnt3zfb8qn
* FastNumber:
  * added ability to get the decimal.
  * added ability to set the decimal.
  * Explicitly define the raw number.
  * Made operatiors public

* OperatorToken:
  * uncommented the operators in init_token ()

* [...]OperatorToken:
  * constructers: protected -> public

* Tests:
  * Added test for FastNumber (need more cases)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
   * Math done on these numbers are done using standard integer operations, and
9
9
   * not floating point math.
10
10
   */
 
11
  
11
12
  public class FastNumber {
12
13
    public const long MUL_FACTOR = 1000;
13
14
    
14
 
    public long raw_number { public get ; private set;}
 
15
    private long real_raw_number;
 
16
    public long raw_number { public get {return real_raw_number;}
 
17
                             private set {real_raw_number = value;}
 
18
    }
15
19
    
16
20
    public long number {
17
 
      public get {return (this.raw_number / MUL_FACTOR);}
18
 
      public set {this.raw_number = (MUL_FACTOR * value);}
 
21
      public get {return (this.real_raw_number / MUL_FACTOR);}
 
22
      public set {this.real_raw_number = (MUL_FACTOR * value);}
 
23
    }
 
24
    
 
25
    public long decimal {
 
26
      public get {return mask_and_normalize_decimal (real_raw_number);}
 
27
      public set {set_decimal_of_number (ref real_raw_number, value);}
19
28
    }
20
29
    
21
30
    public FastNumber (long val = 0) {
60
69
        return new FastNumber.copy (this);
61
70
      }
62
71
      
63
 
      Utils.print_ln ("this: %li, othre: %li", this.raw_number, other.raw_number);
64
 
      
65
72
      var v = new FastNumber ();
66
73
      v.raw_number = (this.raw_number + other.raw_number);
67
74
      return v;
97
104
      return ret;
98
105
    }
99
106
    
 
107
    private static long mask_and_normalize_decimal (long number) {
 
108
      var mask = number / MUL_FACTOR;
 
109
      mask = mask * MUL_FACTOR;
 
110
      return number - mask;
 
111
    }
 
112
    
 
113
    private static void set_decimal_of_number (ref long number, long decimal) {
 
114
      var masked = number / MUL_FACTOR;
 
115
      masked = masked * MUL_FACTOR;
 
116
      number = masked + decimal;
 
117
    }
 
118
    
100
119
  }
101
120
  
102
121
}