/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-09 12:57:20 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210909125720-x6t14nri2anelxp6
Added Round Up and Round Down function classes.
Added ronud up and round down operations to FastNumbers to facitilate this.

TODO: Write tests.

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
  /**
8
14
   * Math done on these numbers are done using standard integer operations, and
9
15
   * not floating point math.
10
16
   */
11
 
  class FastNumber {
 
17
  public struct FastNumber {
12
18
    public const long MUL_FACTOR = 1000;
13
19
    
14
 
    public long raw_number { public get; private set; }
 
20
    /** Precision used to output values */
 
21
    public const int PRECISION_DIGITS = 2;
 
22
    /** Precision factor used to evaluate output */
 
23
    public const int PRECISION_FACTOR = 100;
 
24
    
 
25
    public long raw_number;
15
26
    
16
27
    public long number {
17
 
      get {return raw_number / MUL_FACTOR;}
18
 
      set {raw_number = number * MUL_FACTOR;}
 
28
      public get {return (this.raw_number / MUL_FACTOR);}
 
29
      public set {this.raw_number = (MUL_FACTOR * value);}
 
30
    }
 
31
    
 
32
    public long decimal {
 
33
      public get {return mask_and_normalize_decimal (raw_number);}
 
34
      public set {set_decimal_of_number (ref raw_number, value);}
19
35
    }
20
36
    
21
37
    public FastNumber (long val = 0) {
30
46
      this.raw_number = parse_raw_number (str);
31
47
    }
32
48
    
33
 
    private static long parse_raw_number (string str) {
 
49
    public FastNumber.raw (long raw) {
 
50
      this.raw_number = raw;
 
51
    }
 
52
    
 
53
    public void set_from_string (string str) {
 
54
      this.raw_number = parse_raw_number (str);
 
55
    }
 
56
    
 
57
    public FastNumber add (FastNumber? other) {
 
58
      if (other == null) {
 
59
        return  FastNumber.copy (this);
 
60
      }
 
61
      
 
62
      var v =  FastNumber ();
 
63
      v.raw_number = (this.raw_number + other.raw_number);
 
64
      return v;
 
65
    }
 
66
    
 
67
    public FastNumber subtract (FastNumber? other) {
 
68
      if (other == null) {
 
69
        return  FastNumber.copy (this);
 
70
      }
 
71
      
 
72
      var v =  FastNumber ();
 
73
      v.raw_number = (this.raw_number - other.raw_number);
 
74
      return v;
 
75
    }
 
76
    
 
77
    public FastNumber multiply (FastNumber? other) {
 
78
      if (other == null || other.raw_number == 0) {
 
79
        return  FastNumber ();
 
80
      }
 
81
      
 
82
      var ret =  FastNumber ();
 
83
      ret.raw_number = ((this.raw_number * other.raw_number) / MUL_FACTOR);
 
84
      return ret;
 
85
    }
 
86
    
 
87
    public FastNumber divide (FastNumber other) throws MathError {
 
88
      if (other.raw_number == 0) {
 
89
        throw new MathError.DIVIDE_BY_ZERO
 
90
                                      ("FantNumber - trying to divide by zero");
 
91
      }
 
92
      var ret =  FastNumber ();
 
93
      ret.raw_number = ((this.raw_number * MUL_FACTOR) / other.raw_number);
 
94
      return ret;
 
95
    }
 
96
    
 
97
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
98
    public long compare (FastNumber other) {
 
99
      return this.raw_number - other.raw_number;
 
100
    }
 
101
    
 
102
    public FastNumber round_up () {
 
103
      FastNumber ret;
 
104
      long decimal = raw_number % PRECISION_FACTOR;
 
105
      if (decimal > 0) {
 
106
        ret = FastNumber.raw (raw_number + PRECISION_FACTOR - decimal);
 
107
      } else {
 
108
        ret = FastNumber.raw (raw_number - decimal);
 
109
      }
 
110
      return ret;
 
111
    }
 
112
    
 
113
    public FastNumber round_down () {
 
114
      FastNumber ret;
 
115
      long decimal = raw_number % PRECISION_FACTOR;
 
116
      if (decimal < 0) {
 
117
        // Is this ever reached?
 
118
        ret = FastNumber.raw (raw_number - PRECISION_FACTOR - decimal);
 
119
      } else {
 
120
        ret = FastNumber.raw (raw_number - decimal);
 
121
      }
 
122
      return ret;
 
123
    }
 
124
    
 
125
    public string to_string (bool decimal = false) {
 
126
      if (decimal) {
 
127
        return number.to_string () + "." + decimal.to_string ();
 
128
      } else {
 
129
        return number.to_string ();
 
130
      }
 
131
    } 
 
132
    
 
133
    // ***** STATIC FUNCTIONS ****//
 
134
    public static long parse_raw_number (string str) {
34
135
      long ret_val = 0;
35
136
      int i_of_dot = str.index_of_char ('.');
36
137
      if (i_of_dot >= 0) {
54
155
      return ret_val;
55
156
    }
56
157
    
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
 
    
 
158
    public static long mask_and_normalize_decimal (long number) {
 
159
      var mask = number / MUL_FACTOR;
 
160
      mask = mask * MUL_FACTOR;
 
161
      return number - mask;
 
162
    }
 
163
    
 
164
    public static void set_decimal_of_number (ref long number, long decimal) {
 
165
      var masked = number / MUL_FACTOR;
 
166
      masked = masked * MUL_FACTOR;
 
167
      number = masked + decimal;
 
168
    }
 
169
    
 
170
    [CCode (cname = "vqdr_common_fast_number_compare")]
 
171
    public static extern long static_compare (FastNumber a, FastNumber b);
95
172
  }
96
 
  
97
173
}