1
namespace VQDR.Common {
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.
8
* Math done on these numbers are done using standard integer operations, and
9
* not floating point math.
12
public const long MUL_FACTOR = 1000;
14
public long raw_number { public get; private set; }
17
get {return raw_number / MUL_FACTOR;}
18
set {raw_number = number * MUL_FACTOR;}
21
public FastNumber (long val = 0) {
25
public FastNumber.copy (FastNumber other) {
26
this.raw_number = other.raw_number;
29
public FastNumber.from_string (string str) {
30
this.raw_number = parse_raw_number (str);
33
private static long parse_raw_number (string str) {
35
int i_of_dot = str.index_of_char ('.');
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));
43
// Normalise the digits.
44
while (ret_val > MUL_FACTOR) {
45
ret_val = ret_val / 10;
49
ret_val = ret_val + (long.parse ("0" + str.substring (0, i_of_dot))
52
ret_val = long.parse (str) * MUL_FACTOR;
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);