8
14
* Math done on these numbers are done using standard integer operations, and
9
15
* not floating point math.
17
public struct FastNumber {
12
18
public const long MUL_FACTOR = 1000;
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;
25
public long raw_number;
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);}
33
public get {return mask_and_normalize_decimal (raw_number);}
34
public set {set_decimal_of_number (ref raw_number, value);}
21
37
public FastNumber (long val = 0) {
30
46
this.raw_number = parse_raw_number (str);
33
private static long parse_raw_number (string str) {
49
public FastNumber.raw (long raw) {
50
this.raw_number = raw;
53
public void set_from_string (string str) {
54
this.raw_number = parse_raw_number (str);
57
public FastNumber add (FastNumber? other) {
59
return FastNumber.copy (this);
62
var v = FastNumber ();
63
v.raw_number = (this.raw_number + other.raw_number);
67
public FastNumber subtract (FastNumber? other) {
69
return FastNumber.copy (this);
72
var v = FastNumber ();
73
v.raw_number = (this.raw_number - other.raw_number);
77
public FastNumber multiply (FastNumber? other) {
78
if (other == null || other.raw_number == 0) {
82
var ret = FastNumber ();
83
ret.raw_number = ((this.raw_number * other.raw_number) / MUL_FACTOR);
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");
92
var ret = FastNumber ();
93
ret.raw_number = ((this.raw_number * MUL_FACTOR) / other.raw_number);
97
[CCode (cname = "vqdr_common_fast_number_compare")]
98
public long compare (FastNumber other) {
99
return this.raw_number - other.raw_number;
102
public FastNumber round_up () {
104
long decimal = raw_number % PRECISION_FACTOR;
106
ret = FastNumber.raw (raw_number + PRECISION_FACTOR - decimal);
108
ret = FastNumber.raw (raw_number - decimal);
113
public FastNumber round_down () {
115
long decimal = raw_number % PRECISION_FACTOR;
117
// Is this ever reached?
118
ret = FastNumber.raw (raw_number - PRECISION_FACTOR - decimal);
120
ret = FastNumber.raw (raw_number - decimal);
125
public string to_string (bool decimal = false) {
127
return number.to_string () + "." + decimal.to_string ();
129
return number.to_string ();
133
// ***** STATIC FUNCTIONS ****//
134
public static long parse_raw_number (string str) {
35
136
int i_of_dot = str.index_of_char ('.');
36
137
if (i_of_dot >= 0) {
58
public FastNumber add (FastNumber? other) {
60
return new FastNumber.copy (this);
63
var v = new FastNumber (this.raw_number + other.raw_number);
68
public FastNumber subtract (FastNumber? other) {
70
return new FastNumber.copy (this);
73
var v = new FastNumber (this.raw_number - other.raw_number);
78
public FastNumber multiply (FastNumber? other) {
79
if (other == null || other.raw_value == 0) {
80
return new FastNumber ();
83
return new FastNumber ((this.raw_number * other.raw_number) / MUL_FACTOR);
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");
92
return new FastNumber ((this.raw_number * MUL_FACTOR) / other.raw_number);
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;
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;
170
[CCode (cname = "vqdr_common_fast_number_compare")]
171
public static extern long static_compare (FastNumber a, FastNumber b);