bzr branch
http://gegoxaren.bato24.eu/bzr/vqdr/trunk
|
1
by Gustav Hartvigsson
* Initial code - far from done |
1 |
namespace VQDR.Common { |
2 |
|
|
3 |
/** |
|
4 |
* Fast Numbers are a decimal representanion of numbers in the folm of
|
|
5 |
* a normal integer value. All internal nummbers are multiples of 1000, so the
|
|
6 |
* there is room for two three decimal ponts.
|
|
7 |
*
|
|
8 |
* Math done on these numbers are done using standard integer operations, and
|
|
9 |
* not floating point math.
|
|
10 |
*/
|
|
11 |
class FastNumber { |
|
12 |
public const long MUL_FACTOR = 1000; |
|
13 |
|
|
14 |
public long raw_number { public get; private set; } |
|
15 |
|
|
16 |
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 |
|
|
25 |
public FastNumber.copy (FastNumber other) { |
|
26 |
this.raw_number = other.raw_number; |
|
27 |
} |
|
28 |
|
|
29 |
public FastNumber.from_string (string str) { |
|
30 |
this.raw_number = parse_raw_number (str); |
|
31 |
} |
|
32 |
|
|
33 |
private static long parse_raw_number (string str) { |
|
34 |
long ret_val = 0; |
|
35 |
int i_of_dot = str.index_of_char ('.'); |
|
36 |
if (i_of_dot >= 0) { |
|
37 |
|
|
38 |
// Get the decimal number from the string, if such a thing exists. |
|
39 |
if ((str.length - 1 > i_of_dot)) { |
|
40 |
ret_val = long.parse ((str + "000").substring (i_of_dot + 1)); |
|
41 |
} |
|
42 |
|
|
43 |
// Normalise the digits. |
|
44 |
while (ret_val > MUL_FACTOR) { |
|
45 |
ret_val = ret_val / 10; |
|
46 |
} |
|
47 |
|
|
48 |
// Add intiger number |
|
49 |
ret_val = ret_val + (long.parse ("0" + str.substring (0, i_of_dot)) |
|
50 |
* MUL_FACTOR); |
|
51 |
} else { |
|
52 |
ret_val = long.parse (str) * MUL_FACTOR; |
|
53 |
} |
|
54 |
return ret_val; |
|
55 |
} |
|
56 |
|
|
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 |
|
|
95 |
} |
|
96 |
|
|
97 |
}
|