/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: 2024-12-21 22:39:17 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241221223917-jbt2ylyz9nxjss49
various changes
[utils/utils.vala]
* Removed uneeded int32_abs function

[utils/stack]
* more informative error when trying to pop an empty stack.

[utils/random.vala]
* added c_names to functions (probobly not needed).

[utils/pair.vala]
* Made compact
* made FreeFunc delegates unowned to fix error
* added constructor Pair.with_free_func ().

[utils/named_vector.vala]
* made class compact.

[utils/meson.build]
* Reordered files in list
* added logger.vala.

[utils/logger.vala]
* Added fast and easy logger class.

[utils/fast_number.vala]
* added a bunch of cname CCode attributes.

[general]
* Spelling in comments and functions.

[meson.build]
* Made dependancies easier to read
* added vala posix dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
     * 
63
63
     * @param decimal  The decimal part of the number. Defaults to 0.
64
64
     */
 
65
    [CCode (cname = "v_fast_number_new")]
65
66
    public FastNumber (int64 number = 0) {
66
67
      if (number != 0) {
67
68
        this.raw_number = (number * MUL_FACTOR);
73
74
    /**
74
75
     * Do a deep copy of a FastNumber.
75
76
     */
 
77
    [CCode (cname = "v_fast_number_copy")]
76
78
    public FastNumber.copy (FastNumber other) {
77
79
      this.raw_number = other.raw_number;
78
80
    }
82
84
     * 
83
85
     * Can be a decimal representation.
84
86
     */
 
87
    [CCode (cname = "v_fast_number_new_from_string")]
85
88
    public FastNumber.from_string (string str) {
86
89
      parse_raw_number (str);
87
90
    }
88
91
    /**
89
92
     * Initialises a FastNumber with the internal representation of that number.
90
93
     */
 
94
    [CCode (cname = "v_fast_number_new_raw")]
91
95
    public FastNumber.raw (int64 raw) {
92
96
      this.raw_number = raw;
93
97
    }
94
98
    
 
99
    [CCode (cname = "v_fast_number_new_from_float")]
95
100
    public FastNumber.from_float (double float_number) {
96
101
      // XXX Do we need a faster way of doing this?
97
102
      parse_raw_number (float_number.to_string ()); 
110
115
     * 
111
116
     * @param other The other fast number you want to add to this value.
112
117
     */
 
118
    [CCode (cname = "v_fast_number_add")]
113
119
    public FastNumber add (FastNumber? other) {
114
120
      if (other == null) {
115
121
        return  FastNumber.copy (this);
134
140
     * @param other  The other fast number you want to subtract from this 
135
141
     *               FastNumber.
136
142
     */
 
143
    [CCode (cname = "v_fast_number_subtract")]
137
144
    public FastNumber subtract (FastNumber? other) {
138
145
      if (other == null) {
139
146
        return  FastNumber.copy (this);
157
164
     * 
158
165
     * @param other The value you want to multiply this value with.
159
166
     */
 
167
    [CCode (cname = "v_fast_number_multiply")]
160
168
    public FastNumber multiply (FastNumber? other) {
161
169
      if (other == null || other.raw_number == 0 || this.raw_number == 0) {
162
170
        return  FastNumber ();
180
188
     * 
181
189
     * @param other The value you want to multiply this value with.
182
190
     */
 
191
    [CCode (cname = "v_fast_number_divide")]
183
192
    public FastNumber divide (FastNumber other) throws MathError {
184
193
      if (other.raw_number == 0) {
185
194
        throw new MathError.DIVIDE_BY_ZERO ("trying to divide by zero");
189
198
      return ret;
190
199
    }
191
200
    
192
 
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
201
    [CCode (cname = "v_fast_number_compare")]
193
202
    public int64 compare (FastNumber other) {
194
203
      return this.raw_number - other.raw_number;
195
204
    }
199
208
     * 
200
209
     * @return a rounded up FastNumber.
201
210
     */
 
211
    [CCode (cname = "v_fast_number_round_up")]
202
212
    public FastNumber round_up () {
203
213
      FastNumber ret;
204
214
      int64 decimal = raw_number % PRECISION_FACTOR;
235
245
     * @param decimal whether to return the decimal portion of the number in 
236
246
     *                the string. Default = false.
237
247
     */
 
248
    [CCode (cname = "v_fast_number_to_string")]
238
249
    public string to_string (bool decimal = false) {
239
250
      string ret_val = null;
240
251
      if (!decimal) {
271
282
      return ret_val;
272
283
    }
273
284
    
 
285
    [CCode (cname = "v_fast_number_to_float")]
274
286
    public double to_float () {
275
287
      // XXX This probobly needs to something faster?
276
288
      return double.parse (this.to_string (true));
277
289
    }
278
290
    
 
291
    [CCode (cname = "v_fast_number_to_int")]
279
292
    public int64 to_int () {
280
293
       return (this.raw_number / MUL_FACTOR);
281
294
    }
291
304
    }
292
305
    
293
306
    
294
 
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
307
    [CCode (cname = "v_fast_number_compare")]
295
308
    public static extern int64 static_compare (FastNumber a, FastNumber b);
296
309
    
 
310
    [CCode (cname = "v_fast_number_private_parse_raw")]
297
311
    private void parse_raw_number (string str) {
298
312
      //debug (@"(parse_raw_number) str: $str");
299
313
      int64 ret_val = 0;