-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.js
160 lines (122 loc) · 4.89 KB
/
library.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
var Ratana = Ratana || {};
Ratana.Karn = Ratana.Karn || {};
Ratana.Karn.Euler = Ratana.Karn.Euler || {};
Ratana.Karn.Euler.Library = function () {}
Ratana.Karn.Euler.Library.BigNumber = function (input, base) {
// base of the number
// e.g. 2 = binary, 10 = decimal
this.Base = typeof base !== 'undefined' ? base : 10;
// Value stores each digit in an array with the index being the power of 10.
// e.g. 314,159 is stored as Value = [9,5,1,4,1,3];
// 9 x 10^0 = 9
// 5 x 10^1 = 50
// 1 x 10^2 = 100
// 4 x 10^3 = 4,000
// 1 x 10^4 = 10,000
// 3 x 10^5 = 300,000
this.Value = [];
// if input is number, convert it to string so we can get each digit
// then set the values
if (!isNaN(Number(input))) {
var inputString = input + "";
for (var ii = inputString.length - 1; ii >= 0; ii--) {
this.Value.push(parseInt(inputString[ii]));
}
}
// if not, set it to 0
else {
this.Value.push(0);
}
}
with ({
$: Ratana.Karn.Euler.Library.BigNumber,
o: Ratana.Karn.Euler.Library.BigNumber.prototype
}) {
o.toString = function () {
var result = "";
for (var ii = this.Value.length - 1; ii >= 0; ii--) {
result += this.Value[ii];
}
return result;
},
// static methods
$.add = function(a, b) {
// make sure a, b are instance of BigNumber
if (!(a instanceof $)) a = new $(a);
if (!(b instanceof $)) b = new $(b);
// make sure the bases match
if (a.Base != b.Base) throw "Bases of the numbers do not match.";
var result = new $();
// loop through all the digits
var carryOver = 0;
for (var ii = 0; ii < Math.max(a.Value.length, b.Value.length); ii++) {
// if one number is shorter then the other default its digit to 0
var aii = isNaN(a.Value[ii]) ? 0 : a.Value[ii];
var bii = isNaN(b.Value[ii]) ? 0 : b.Value[ii];
// do the actual addition
result.Value[ii] = aii + bii + carryOver;
// calculate the carry over value
if (result.Value[ii] >= a.Base) {
carryOver = Math.floor(result.Value[ii] / a.Base);
result.Value[ii] = result.Value[ii] % a.Base;
} else {
carryOver = 0;
}
}
// add the last carry over
if (carryOver > 0) result.Value.push(carryOver);
return result;
}
$.multiply = function(a, b) {
// make sure a, b are instance of BigNumber
if (!(a instanceof $)) a = new $(a);
if (!(b instanceof $)) b = new $(b);
// make sure the bases match
if (a.Base != b.Base) throw "Bases of the numbers do not match.";
var result = new $();
var products = [];
for (var ii in a.Value) {
var tempProduct = new $();
var carryOver = 0;
for (var jj in b.Value) {
// if one number is shorter then the other default its digit to 0
var aii = isNaN(a.Value[ii]) ? 0 : a.Value[ii];
var bjj = isNaN(b.Value[jj]) ? 0 : b.Value[jj];
// do the actual multiplication
tempProduct.Value[jj] = (aii * bjj) + carryOver;
if (tempProduct.Value[jj] >= a.Base) {
carryOver = Math.floor(tempProduct.Value[jj] / a.Base);
tempProduct.Value[jj] = tempProduct.Value[jj] % a.Base;
} else {
carryOver = 0;
}
}
// add the last carry over
if (carryOver > 0) tempProduct.Value.push(carryOver);
// each position for a goes up by 1 order of magnitude
tempProduct = $.shiftUp(tempProduct, ii);
products.push(tempProduct);
}
// add all the products to get the result
for (var ii in products) {
result = $.add(result, products[ii]);
}
return result;
}
$.shiftUp = function(a, places) {
// this method multiplies a by this.Base^places
if (!(a instanceof $)) a = new $(a);
// if places to shift is not a number, throw an error
if (isNaN(parseInt(places))) throw "Invalid places to shift up.";
var result = new $();
// add 0's (starts at 1 because result is already initialized with 0)
for (var ii=1; ii<places; ii++) {
result.Value.push(0);
}
// append the values from a
for (var ii in a.Value) {
result.Value.push(a.Value[ii]);
}
return result;
}
}