/
num.js
275 lines (213 loc) · 5.93 KB
/
num.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
var int = require('int');
function Num(num, prec) {
if (!(this instanceof Num)) {
return new Num(num, prec);
}
var self = this;
if (num instanceof Num) {
self._int = int(num._int);
self._precision = num._precision;
return self;
}
if (num instanceof int) {
this._int = num;
this._precision = prec || 0;
return self;
}
// convert to a string
num = '' + num;
// find Num point
var dec = num.indexOf('.');
if (dec >= 0) {
// take out the Num point
num = num.replace('.', '');
var precision = num.length - dec;
}
else {
var precision = 0;
}
this._int = int(num);
this._precision = precision;
if (prec && prec > 0) {
this.set_precision(prec);
}
}
// TODO (shtylman) cleanup
Num.prototype.toString = function() {
var num_str = this._int.toString();
// 0 precision, just return the int
if (this._precision <= 0) {
return num_str;
}
// if number is negative, store that and make it positive
var neg = false;
if (num_str.charAt(0) === '-') {
// negative
neg = true;
num_str = num_str.slice(1);
}
// find index where to add the decimal point
var idx = num_str.length - this._precision;
// insert the proper number of 0s after the .
if(idx < 0) {
var zeros = new Array(-idx + 1).join('0');
idx = 0;
} else {
var zeros = '';
}
// make sure there's always a number before the .
var before_dot = idx > 0 ? num_str.slice(0, idx) : '0';
return (neg ? '-' : '') + before_dot + '.' + zeros + num_str.slice(idx);
};
Num.prototype.valueOf = Num.prototype.toString;
Num.prototype.toJSON = Num.prototype.toString;
/// return {int} the precision
Num.prototype.get_precision = function() {
return this._precision;
};
/// setting precision to < current precision will floor, NOT round
/// modifies this object
Num.prototype.set_precision = function(precision) {
var self = this;
var precision_diff = precision - self._precision;
if (precision_diff > 0) {
self._int = self._int.mul(int(10).pow(precision_diff));
}
else if(precision_diff < 0) {
self._int = self._int.div(int(10).pow(-precision_diff));
}
self._precision += precision_diff;
return self;
};
/// returns a copy
Num.prototype.round = function(precision) {
var copy = Num(this);
if (precision >= copy._precision) {
return copy;
}
var num_str = copy._int.toString();
// the index to check for rounding
var idx = num_str.length - copy._precision + precision;
// the number to check for rounding
var n = num_str[idx] - 0;
var neg = num_str[0] === '-';
copy.set_precision(precision);
if ((neg && n < 5) || (!neg && n >= 5)) {
copy._int = copy._int.add(1);
}
return copy;
};
/// returns new Num, -this
Num.prototype.neg = function() {
return new Num(this._int.neg(), this._precision);
};
/// returns new Num, absolute value of this
Num.prototype.abs = function() {
return new Num(this._int.abs(), this._precision);
};
/// returns a + b
/// a, b can each be either a Num, String, or Number
/// will return a new Num with the greatest precision of the operands
Num.add = function(a, b) {
a = ensure_num(a);
b = ensure_num(b);
var precision = Math.max(a._precision, b._precision);
var a = Num(a);
a.set_precision(precision);
var b = Num(b);
b.set_precision(precision);
// the integer result
var num_res = a._int.add(b._int);
return new Num(num_res, precision);
};
/// returns a - b
/// a, b can each be either a Num, String, or Number
/// will return a new Num with the greatest precision of the operands
Num.sub = function(a, b) {
b = Num(b); // convert/copy before modifying
b._int = b._int.neg(); // negate
return Num.add(a, b);
};
/// returns a * b
/// a, b can each be either a Num, String, or Number
/// will return a new Num with precision = a.precision + b.precision
Num.mul = function(a, b) {
a = ensure_num(a);
b = ensure_num(b);
return new Num(a._int.mul(b._int), a._precision + b._precision);
};
Num.div = function(a, b) {
a = ensure_num(a);
b = ensure_num(b);
var a_int = a._int;
var precision = b._precision;
for (var i=0 ; i<precision ; ++i) {
a_int = a_int.mul(10);
}
return Num(a_int.div(b._int), a._precision);
};
Num.mod = function(a, b) {
a = ensure_num(a);
b = ensure_num(b);
var prec_a = a._precision;
var prec_b = b._precision;
var a = a._int;
var b = b._int;
while (prec_a < prec_b) {
a = a.mul(10);
prec_a += 1;
}
while (prec_b < prec_a) {
b = b.mul(10);
prec_b += 1;
}
return Num(a.mod(b), prec_a);
};
/// returns < 0 if a < b, 0 if a == b, > 0 if a > b
Num.cmp = function(a, b) {
// make copies cause we modify precision
var a = Num(a);
var b = Num(b);
if (a._int._s != b._int._s) {
return a._int._s ? -1 : 1;
}
// normalize the two numbers
var precision = Math.max(a._precision, b._precision);
a.set_precision(precision);
b.set_precision(precision);
return a._int.cmp(b._int);
};
Num.eq = function(a, b) {
return Num.cmp(a, b) === 0;
};
Num.gt = function(a, b) {
return Num.cmp(a, b) > 0;
};
Num.gte = function(a, b) {
return Num.cmp(a, b) >= 0;
};
Num.lt = function(a, b) {
return Num.cmp(a, b) < 0;
};
Num.lte = function(a, b) {
return Num.cmp(a, b) <= 0;
};
// add all the static methods in Num to Num's prototype
(function() {
function add_method(name) {
Num.prototype[name] = function(b) {
return Num[name](this, b);
}
}
for(var i in Num) {
add_method(i);
}
})();
/// box the value into a Num if it isn't
function ensure_num(val) {
if (val instanceof Num) {
return val;
}
return Num(val);
}
module.exports = Num;