/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-09-11 16:26:54 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210911162654-rfd097o28o24fxeb
* Mandate ABI stability.
* Make sure comments are avalible in the vapi file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contects of this file is in the Public Domain.
 
3
 *
 
4
 * Created by Gustav Hartivgsson.
 
5
 */
 
6
 
1
7
namespace VQDR.Common {
2
8
  
3
9
  /**
7
13
   * 
8
14
   * Math done on these numbers are done using standard integer operations, and
9
15
   * not floating point math.
 
16
   * 
 
17
   * The decimal part of the FastNumber has a maximum of 3 decimals.
10
18
   */
11
 
  class FastNumber {
12
 
    public const long MUL_FACTOR = 1000;
13
 
    
14
 
    public long raw_number { public get; private set; }
 
19
  public struct FastNumber {
 
20
    public const int MUL_FACTOR = 1000;
 
21
    
 
22
    /** Precision used to output values */
 
23
    public const int PRECISION_DIGITS = 2;
 
24
    /** Precision factor used to evaluate output */
 
25
    public const int PRECISION_FACTOR = 100;
 
26
    
 
27
    public long raw_number;
15
28
    
16
29
    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
 
    }
24
 
    
 
30
      public get {return (this.raw_number / MUL_FACTOR);}
 
31
      public set {this.raw_number = (MUL_FACTOR * value);}
 
32
    }
 
33
    
 
34
    public long decimal {
 
35
      public get {return mask_and_normalize_decimal (raw_number);}
 
36
      public set {set_decimal_of_number (ref raw_number, value);}
 
37
    }
 
38
    
 
39
    public double float_rep {
 
40
      public get {return double.parse (@"$number.$decimal");}
 
41
      public set {this.raw_number = parse_raw_number (value.to_string ());}
 
42
    }
 
43
    
 
44
    /**
 
45
     * Initialises a FastNumber.
 
46
     * 
 
47
     * @param number   The number that are to be set as the none-decimal part of
 
48
     *                 the number. Defaults to 0.
 
49
     * 
 
50
     * @param decimal  The decimal part of the number. Defaults to 0.
 
51
     */
 
52
    public FastNumber (long number = 0, int decimal = 0) {
 
53
      this.number = number;
 
54
      this.decimal = decimal;
 
55
    }
 
56
    
 
57
    /**
 
58
     * Do a deep copy of a FastNumber.
 
59
     */
25
60
    public FastNumber.copy (FastNumber other) {
26
61
      this.raw_number = other.raw_number;
27
62
    }
28
63
    
 
64
    /**
 
65
     * Initialises a FastNumber from a string.
 
66
     * 
 
67
     * Can be a decimal representation.
 
68
     */
29
69
    public FastNumber.from_string (string str) {
30
70
      this.raw_number = parse_raw_number (str);
31
71
    }
32
72
    
33
 
    private static long parse_raw_number (string str) {
 
73
    /**
 
74
     * Initialises a FastNumber from a double floating point value.
 
75
     * 
 
76
     * Due to how floatinng point numbers works this may not be the exact value
 
77
     * you expect it to be.
 
78
     */
 
79
    public FastNumber.from_float (double f) {
 
80
      this.raw_number = parse_raw_number (f.to_string ());
 
81
    }
 
82
    
 
83
    /**
 
84
     * Initialises a FastNumber with the internal representation of that number.
 
85
     */
 
86
    public FastNumber.raw (long raw) {
 
87
      this.raw_number = raw;
 
88
    }
 
89
    
 
90
    /**
 
91
     * Sets the value of this FastNumber from a string, 
 
92
     */
 
93
    public void set_from_string (string str) {
 
94
      this.raw_number = parse_raw_number (str);
 
95
    }
 
96
    
 
97
    /**
 
98
     * Add this to an other FastNumber.
 
99
     * 
 
100
     * {{{
 
101
     * var f1 = FastNumber (3);   // f1 = 3
 
102
     * var f2 = FastNumber (2);   // f2 = 2
 
103
     * var f3 = f1.add (f2);      // f3 = 5
 
104
     * }}}
 
105
     * 
 
106
     * @return a newly inisialised FastNumber.
 
107
     * 
 
108
     * @param other The other fast number you want to add to this value.
 
109
     */
 
110
    public FastNumber add (FastNumber? other) {
 
111
      if (other == null) {
 
112
        return  FastNumber.copy (this);
 
113
      }
 
114
      
 
115
      var v = FastNumber ();
 
116
      v.raw_number = (this.raw_number + other.raw_number);
 
117
      return v;
 
118
    }
 
119
    
 
120
    /**
 
121
     * Add this to an other FastNumber.
 
122
     * 
 
123
     * {{{
 
124
     * var f1 = FastNumber (3);   // f1 = 3
 
125
     * var f2 = FastNumber (2);   // f2 = 2
 
126
     * var f3 = f1.subtract (f2); // f3 = 1
 
127
     * }}}
 
128
     * 
 
129
     * @return a newly inisialised FastNumber.
 
130
     * 
 
131
     * @param other  The other fast number you want to subtract from this 
 
132
     *               FastNumber.
 
133
     */
 
134
    public FastNumber subtract (FastNumber? other) {
 
135
      if (other == null) {
 
136
        return  FastNumber.copy (this);
 
137
      }
 
138
      
 
139
      var v = FastNumber ();
 
140
      v.raw_number = (this.raw_number - other.raw_number);
 
141
      return v;
 
142
    }
 
143
    
 
144
    /**
 
145
     * Multiply this FastNumber with another FastNumber.
 
146
     * 
 
147
     * {{{
 
148
     * var f1 = FastNumber (3);   // f1 = 3
 
149
     * var f2 = FastNumber (2);   // f2 = 2
 
150
     * var f3 = f1.multiply (f2); // f3 = 6
 
151
     * }}}
 
152
     * 
 
153
     * @return a newly initalised FastNumber.
 
154
     * 
 
155
     * @param other The value you want to multiply this value with.
 
156
     */
 
157
    public FastNumber multiply (FastNumber? other) {
 
158
      if (other == null || other.raw_number == 0 || this.raw_number == 0) {
 
159
        return  FastNumber ();
 
160
      }
 
161
      
 
162
      var ret = FastNumber ();
 
163
      ret.raw_number = ((this.raw_number * other.raw_number) / MUL_FACTOR);
 
164
      return ret;
 
165
    }
 
166
    
 
167
    /**
 
168
     * Divide this FastNumbers with anothr FastNumber.
 
169
     * 
 
170
     * {{{
 
171
     * var f1 = FastNumber (6);   // f1 = 6
 
172
     * var f2 = FastNumber (2);   // f2 = 2
 
173
     * var f3 = f1.multiply (f2); // f3 = 3
 
174
     * }}}
 
175
     * 
 
176
     * @return a newly initalised FastNumber.
 
177
     * 
 
178
     * @param other The value you want to multiply this value with.
 
179
     */
 
180
    public FastNumber divide (FastNumber other) throws MathError {
 
181
      if (other.raw_number == 0) {
 
182
        throw new MathError.DIVIDE_BY_ZERO
 
183
                                      ("FantNumber - trying to divide by zero");
 
184
      }
 
185
      var ret =  FastNumber ();
 
186
      ret.raw_number = ((this.raw_number * MUL_FACTOR) / other.raw_number);
 
187
      return ret;
 
188
    }
 
189
    
 
190
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
191
    public long compare (FastNumber other) {
 
192
      return this.raw_number - other.raw_number;
 
193
    }
 
194
    
 
195
    /**
 
196
     * Round up this FastNumber and retuns it.
 
197
     * 
 
198
     * @return a rounded up FastNumber.
 
199
     */
 
200
    public FastNumber round_up () {
 
201
      FastNumber ret;
 
202
      long decimal = raw_number % PRECISION_FACTOR;
 
203
      if (decimal > 0) {
 
204
        ret = FastNumber.raw (raw_number + PRECISION_FACTOR - decimal);
 
205
      } else {
 
206
        ret = FastNumber.raw (raw_number - decimal);
 
207
      }
 
208
      return ret;
 
209
    }
 
210
    
 
211
    /**
 
212
     * Round up this FastNumber and retuns it.
 
213
     * 
 
214
     * @return a rounded down FastNumber.
 
215
     */
 
216
    public FastNumber round_down () {
 
217
      FastNumber ret;
 
218
      long decimal = raw_number % PRECISION_FACTOR;
 
219
      if (decimal < 0) {
 
220
        // Is this ever reached?
 
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
     * FastNumber to string.
 
230
     * 
 
231
     * @return a string
 
232
     * 
 
233
     * @param decimal whether to return the decimal portion of the number in 
 
234
     *                the string. Default = false.
 
235
     */
 
236
    public string to_string (bool decimal = false) {
 
237
      if (decimal) {
 
238
        return @"$number.$decimal";
 
239
      } else {
 
240
        return number.to_string ();
 
241
      }
 
242
    } 
 
243
    
 
244
    // ***** STATIC FUNCTIONS ****//
 
245
    public static long parse_raw_number (string str) {
34
246
      long ret_val = 0;
35
247
      int i_of_dot = str.index_of_char ('.');
36
248
      if (i_of_dot >= 0) {
37
 
      
 
249
        
 
250
        debug (@"str: $str");
 
251
        
38
252
        // Get the decimal number from the string, if such a thing exists.
39
253
        if ((str.length - 1 > i_of_dot)) {
40
254
          ret_val = long.parse ((str + "000").substring (i_of_dot + 1));
41
255
        }
42
256
        
 
257
        debug (@"i_of_dot: $i_of_dot, ret_val (decimal): $ret_val\n");
 
258
        
43
259
        // Normalise the digits.
44
260
        while (ret_val > MUL_FACTOR) {
45
261
          ret_val = ret_val / 10;
46
262
        }
47
263
        
 
264
        debug (@"ret_val (normalised): $ret_val\n");
 
265
        
48
266
        // Add intiger number
49
 
        ret_val = ret_val + (long.parse ("0" + str.substring (0, i_of_dot))
 
267
        ret_val = ret_val + (long.parse (str.substring (0, i_of_dot))
50
268
                            * MUL_FACTOR);
 
269
        
 
270
        debug (@"ret_val (finised): $ret_val\n");
 
271
        
51
272
      } else {
52
273
        ret_val = long.parse (str) * MUL_FACTOR;
53
274
      }
54
275
      return ret_val;
55
276
    }
56
277
    
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
 
    
 
278
    public static long mask_and_normalize_decimal (long number) {
 
279
      var mask = number / MUL_FACTOR;
 
280
      mask = mask * MUL_FACTOR;
 
281
      return number - mask;
 
282
    }
 
283
    
 
284
    public static void set_decimal_of_number (ref long number, long decimal) {
 
285
      var masked = number / MUL_FACTOR;
 
286
      masked = masked * MUL_FACTOR;
 
287
      number = masked + decimal;
 
288
    }
 
289
    
 
290
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
291
    public static extern long static_compare (FastNumber a, FastNumber b);
95
292
  }
96
 
  
97
293
}