8
8
* Math done on these numbers are done using standard integer operations, and
9
9
* not floating point math.
11
public class FastNumber {
12
12
public const long MUL_FACTOR = 1000;
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;}
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);}
25
public get {return mask_and_normalize_decimal (real_raw_number);}
26
public set {set_decimal_of_number (ref real_raw_number, value);}
21
29
public FastNumber (long val = 0) {
25
33
public FastNumber.copy (FastNumber other) {
26
this.raw_number = other.raw_number;
34
this.real_raw_number = other.real_raw_number;
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);
33
41
private static long parse_raw_number (string str) {
70
78
return new FastNumber.copy (this);
73
var v = new FastNumber (this.raw_number - other.raw_number);
81
var v = new FastNumber ();
82
v.raw_number = (this.real_raw_number - other.real_raw_number);
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 ();
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);
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");
92
return new FastNumber ((this.raw_number * MUL_FACTOR) / other.raw_number);
101
var ret = new FastNumber ();
102
ret.raw_number = ((this.real_raw_number * MUL_FACTOR) / other.real_raw_number);
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;
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;