/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 19:32:41 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20210524193241-zgalq2lc1eghdrnu
* fixed spelling error in meson.build

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
 
  class FastNumber {
 
11
  public class FastNumber {
12
12
    public const long MUL_FACTOR = 1000;
13
13
    
14
 
    public long raw_number { public get; private set; }
 
14
    protected long real_raw_number;
 
15
    public long raw_number { public get {return real_raw_number;}
 
16
                             private set {real_raw_number = value;}
 
17
    }
15
18
    
16
19
    public long number {
17
 
      get {return raw_number / MUL_FACTOR;}
18
 
      set {raw_number = number * MUL_FACTOR;}
 
20
      public get {return (this.real_raw_number / MUL_FACTOR);}
 
21
      public set {this.real_raw_number = (MUL_FACTOR * value);}
 
22
    }
 
23
    
 
24
    public long decimal {
 
25
      public get {return mask_and_normalize_decimal (real_raw_number);}
 
26
      public set {set_decimal_of_number (ref real_raw_number, value);}
19
27
    }
20
28
    
21
29
    public FastNumber (long val = 0) {
23
31
    }
24
32
    
25
33
    public FastNumber.copy (FastNumber other) {
26
 
      this.raw_number = other.raw_number;
 
34
      this.real_raw_number = other.real_raw_number;
27
35
    }
28
36
    
29
37
    public FastNumber.from_string (string str) {
30
 
      this.raw_number = parse_raw_number (str);
 
38
      this.real_raw_number = parse_raw_number (str);
31
39
    }
32
40
    
33
41
    private static long parse_raw_number (string str) {
60
68
        return new FastNumber.copy (this);
61
69
      }
62
70
      
63
 
      var v = new FastNumber (this.raw_number + other.raw_number);
64
 
      
 
71
      var v = new FastNumber ();
 
72
      v.raw_number = (this.real_raw_number + other.real_raw_number);
65
73
      return v;
66
74
    }
67
75
    
70
78
        return new FastNumber.copy (this);
71
79
      }
72
80
      
73
 
      var v = new FastNumber (this.raw_number - other.raw_number);
74
 
      
 
81
      var v = new FastNumber ();
 
82
      v.raw_number = (this.real_raw_number - other.real_raw_number);
75
83
      return v;
76
84
    }
77
85
    
78
86
    public FastNumber multiply (FastNumber? other) {
79
 
      if (other == null || other.raw_value == 0) {
 
87
      if (other == null || other.real_raw_number == 0) {
80
88
        return new FastNumber ();
81
89
      }
82
90
      
83
 
      return new FastNumber ((this.raw_number * other.raw_number) / MUL_FACTOR);
 
91
      var ret = new FastNumber ();
 
92
      ret.raw_number = ((this.real_raw_number * other.real_raw_number) / MUL_FACTOR);
 
93
      return ret;
84
94
    }
85
95
    
86
96
    public FastNumber divide (FastNumber? other) throws MathError {
87
 
      if (other.raw_number == 0) {
 
97
      if (other.real_raw_number == 0) {
88
98
        throw new MathError.DIVIDE_BY_ZERO
89
99
                                      ("FantNumber - trying to divide by zero");
90
100
      }
91
 
      
92
 
      return new FastNumber ((this.raw_number * MUL_FACTOR) / other.raw_number);
93
 
    }
94
 
    
 
101
      var ret = new FastNumber ();
 
102
      ret.raw_number = ((this.real_raw_number * MUL_FACTOR) / other.real_raw_number);
 
103
      return ret;
 
104
    }
 
105
    
 
106
    private static long mask_and_normalize_decimal (long number) {
 
107
      var mask = number / MUL_FACTOR;
 
108
      mask = mask * MUL_FACTOR;
 
109
      return number - mask;
 
110
    }
 
111
    
 
112
    private static void set_decimal_of_number (ref long number, long decimal) {
 
113
      var masked = number / MUL_FACTOR;
 
114
      masked = masked * MUL_FACTOR;
 
115
      number = masked + decimal;
 
116
    }
95
117
  }
96
 
  
97
118
}