Total Complexity | 202 |
Complexity/F | 3.21 |
Lines of Code | 1020 |
Function Count | 63 |
Duplicated Lines | 160 |
Ratio | 15.69 % |
Changes | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like src/index.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
7 | (function (scope) { |
||
8 | //limits for faster solving |
||
9 | var limits = { |
||
10 | decimalDigits: 200, |
||
11 | integerDigits: 200, |
||
12 | roundDecimals: 15 |
||
13 | }; |
||
14 | |||
15 | //splice function for string |
||
16 | function splice(str, start, delCount, newSubStr) { |
||
17 | return str.slice(0, start) + newSubStr + str.slice(start + Math.abs(delCount)); |
||
18 | } |
||
19 | |||
20 | //function signFix |
||
21 | function signFix(n) { |
||
22 | var sign = ""; |
||
23 | var l = n.length; |
||
24 | var i; |
||
25 | for (i = 0; i < l; i++) { |
||
26 | if (n[i] === "-") { |
||
27 | if (sign === "") { |
||
28 | sign = "-"; |
||
29 | } else { |
||
30 | sign = ""; |
||
31 | } |
||
32 | } else { |
||
33 | break; |
||
34 | } |
||
35 | } |
||
36 | return sign + n.replace(/-/g, ""); |
||
37 | } |
||
38 | |||
39 | //floor function |
||
40 | function floor(n) { |
||
41 | if (n.indexOf(".") === -1) { |
||
42 | return n; |
||
43 | } |
||
44 | return n.split(".")[0]; |
||
45 | } |
||
46 | |||
47 | //removes decimals |
||
48 | function removeDecimal(str) { |
||
49 | return str.replace(/\./g, ""); |
||
50 | } |
||
51 | |||
52 | //returns the sign of the number<string> |
||
53 | function getSign(a) { |
||
54 | if (a[0] !== "-") { |
||
55 | return ""; |
||
56 | } |
||
57 | return "-"; |
||
58 | } |
||
59 | |||
60 | //absolute value of a number<string> |
||
61 | function abs(num) { |
||
62 | if (getSign(num) === "-") { |
||
63 | return num.substr(1, num.length); |
||
64 | } |
||
65 | return num; |
||
66 | } |
||
67 | |||
68 | //pair only decimal |
||
69 | //forrmats two numbers and returns an array |
||
70 | function pairDecimal(a, b, skip) { |
||
71 | a = a.split("."); |
||
72 | b = b.split("."); |
||
73 | |||
74 | a[1] = (a.length - 1) === 1 ? a[1] : "0"; |
||
75 | b[1] = (b.length - 1) === 1 ? b[1] : "0"; |
||
76 | |||
77 | if (!skip) { |
||
78 | var bla = a[1].length; |
||
79 | var blb = b[1].length; |
||
80 | |||
81 | while (bla > blb) { |
||
82 | b[1] += "0"; |
||
83 | blb += 1; |
||
84 | } |
||
85 | while (bla < blb) { |
||
86 | a[1] += "0"; |
||
87 | bla += 1; |
||
88 | } |
||
89 | } |
||
90 | return [a.join("."), b.join(".")]; |
||
91 | } |
||
92 | |||
93 | //chunk piece refactoring minNeg_Max_chunk |
||
94 | View Code Duplication | function minNeg_Max_chunk(_a, _b, _sign){ |
|
|
|||
95 | var al = _a.length; |
||
96 | var bl = _b.length; |
||
97 | if (al > bl) { |
||
98 | return _sign + _a; |
||
99 | } |
||
100 | if (al < bl) { |
||
101 | return _sign + _b; |
||
102 | } |
||
103 | if (al === bl) { |
||
104 | for (var i = 0; i < al; i++) { |
||
105 | if (_a[i] * 1 < _b[i] * 1) { |
||
106 | return _sign + _b; |
||
107 | } else if (_a[i] * 1 > _b[i] * 1) { |
||
108 | return _sign + _a; |
||
109 | } |
||
110 | } |
||
111 | } |
||
112 | return _a; |
||
113 | } |
||
114 | |||
115 | //chunk piece refactoring maxNeg_Min_Chunk |
||
116 | View Code Duplication | function maxNeg_Min_Chunk(_a, _b, _sign){ |
|
117 | var al = _a.length; |
||
118 | var bl = _b.length; |
||
119 | if (al < bl) { |
||
120 | return _sign + _a; |
||
121 | } |
||
122 | if (al > bl) { |
||
123 | return _sign + _b; |
||
124 | } |
||
125 | if (al === bl) { |
||
126 | for (var i = 0; i < al; i++) { |
||
127 | if (_a[i] * 1 > _b[i] * 1) { |
||
128 | return _sign + _b; |
||
129 | } else if (_a[i] * 1 < _b[i] * 1) { |
||
130 | return _sign + _a; |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | return _a; |
||
135 | } |
||
136 | |||
137 | //minimum negative |
||
138 | function minNeg(_a, _b) { |
||
139 | _a = removeLeadingZeroes(_a); |
||
140 | _b = removeLeadingZeroes(_b); |
||
141 | return minNeg_Max_chunk(_a, _b, '-'); |
||
142 | } |
||
143 | |||
144 | //minimum |
||
145 | function min(_a, _b) { |
||
146 | var signa, signb; |
||
147 | _a = removeLeadingZeroes(_a); |
||
148 | _b = removeLeadingZeroes(_b); |
||
149 | signa = getSign(_a); |
||
150 | signb = getSign(_b); |
||
151 | if (signa === "-" && signb !== "-") { |
||
152 | return _a; |
||
153 | } |
||
154 | if (signa !== "-" && signb === "-") { |
||
155 | return _b; |
||
156 | } |
||
157 | if (signa === "-" && signb === "-") { |
||
158 | return minNeg(abs(_a), abs(_b)); |
||
159 | } |
||
160 | return maxNeg_Min_Chunk(_a, _b, ''); |
||
161 | } |
||
162 | |||
163 | //maximum neg |
||
164 | function maxNeg(_a, _b) { |
||
165 | _a = removeLeadingZeroes(_a); |
||
166 | _b = removeLeadingZeroes(_b); |
||
167 | return maxNeg_Min_Chunk(_a, _b, '-'); |
||
168 | } |
||
169 | |||
170 | //maximum |
||
171 | function max(_a, _b) { |
||
172 | var signa, signb; |
||
173 | _a = removeLeadingZeroes(_a); |
||
174 | _b = removeLeadingZeroes(_b); |
||
175 | signa = getSign(_a); |
||
176 | signb = getSign(_b); |
||
177 | if (signa === "-" && signb !== "-") { |
||
178 | return _b; |
||
179 | } |
||
180 | if (signa !== "-" && signb === "-") { |
||
181 | return _a; |
||
182 | } |
||
183 | if (signa === "-" && signb === "-") { |
||
184 | return maxNeg(abs(_a), abs(_b)); |
||
185 | } |
||
186 | return minNeg_Max_chunk(_a, _b, ''); |
||
187 | } |
||
188 | |||
189 | //remove leading zeroes |
||
190 | function removeLeadingZeroes(str) { |
||
191 | str += ""; |
||
192 | var mark = 0; |
||
193 | var strl, i; |
||
194 | str = str.split(""); |
||
195 | strl = str.length; |
||
196 | for (i = 0; i < strl; i++) { |
||
197 | if (str[i] !== "0") { |
||
198 | mark = i; |
||
199 | break; |
||
200 | } |
||
201 | } |
||
202 | if (str[i] === ".") { |
||
203 | mark -= 1; |
||
204 | } |
||
205 | for (var j = 0; j < mark; j++) { |
||
206 | str[j] = ""; |
||
207 | } |
||
208 | return str.join(""); |
||
209 | } |
||
210 | |||
211 | //remove trailing zeroes |
||
212 | function removeTrailingZeroes(str) { |
||
213 | str += ""; |
||
214 | var mark = 0; |
||
215 | var strl, i; |
||
216 | str = str.split("").reverse(); |
||
217 | strl = str.length; |
||
218 | for (i = 0; i < strl; i++) { |
||
219 | if (str[i] !== "0") { |
||
220 | mark = i; |
||
221 | break; |
||
222 | } |
||
223 | } |
||
224 | if (str[i] === ".") { |
||
225 | mark -= 1; |
||
226 | } |
||
227 | for (var j = 0; j < mark; j++) { |
||
228 | str[j] = ""; |
||
229 | } |
||
230 | return str.reverse().join(""); |
||
231 | } |
||
232 | |||
233 | //produce zero <string> |
||
234 | function zero(i) { |
||
235 | if (i <= 0) { |
||
236 | return ""; |
||
237 | } |
||
238 | var str = ""; |
||
239 | while (i !== 0) { |
||
240 | str += "0"; |
||
241 | i--; |
||
242 | } |
||
243 | return str; |
||
244 | } |
||
245 | |||
246 | function isMin(_a, _b, strict) { |
||
247 | var al, bl, i; |
||
248 | _a = removeLeadingZeroes(_a); |
||
249 | _b = removeLeadingZeroes(_b); |
||
250 | if (_a[0] === "-" && _b[0] !== "-") { |
||
251 | return true; |
||
252 | } |
||
253 | if (_a[0] !== "-" && _b[0] === "-") { |
||
254 | return false; |
||
255 | } |
||
256 | if (_a[0] === "-" && _b[0] === "-") { |
||
257 | return isMax(_a.substr(1, _a.length), _b.substr(1, _b.length)); |
||
258 | } |
||
259 | al = _a.length; |
||
260 | bl = _b.length; |
||
261 | if (al < bl) { |
||
262 | return true; |
||
263 | } |
||
264 | if (al > bl) { |
||
265 | return false; |
||
266 | } |
||
267 | if (al === bl) { |
||
268 | for (i = 0; i < al; i++) { |
||
269 | if (_a[i] * 1 > _b[i] * 1) { |
||
270 | return false; |
||
271 | } else if (_a[i] * 1 < _b[i] * 1) { |
||
272 | return true; |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | if (strict) { |
||
277 | return false; |
||
278 | } |
||
279 | return true; |
||
280 | } |
||
281 | |||
282 | //maximum |
||
283 | function isMax(_a, _b, strict) { |
||
284 | var al, bl, i; |
||
285 | _a = removeLeadingZeroes(_a); |
||
286 | _b = removeLeadingZeroes(_b); |
||
287 | if (_a[0] === "-" && _b[0] !== "-") { |
||
288 | return false; |
||
289 | } |
||
290 | if (_a[0] !== "-" && _b[0] === "-") { |
||
291 | return true; |
||
292 | } |
||
293 | if (_a[0] === "-" && _b[0] === "-") { |
||
294 | return isMin(_a.substr(1, _a.length), _b.substr(1, _b.length)); |
||
295 | } |
||
296 | al = _a.length; |
||
297 | bl = _b.length; |
||
298 | if (al > bl) { |
||
299 | return true; |
||
300 | } |
||
301 | if (al < bl) { |
||
302 | return false; |
||
303 | } |
||
304 | if (al === bl) { |
||
305 | for (i = 0; i < al; i++) { |
||
306 | if (_a[i] * 1 < _b[i] * 1) { |
||
307 | return false; |
||
308 | } else if (_a[i] * 1 > _b[i] * 1) { |
||
309 | return true; |
||
310 | } |
||
311 | } |
||
312 | } |
||
313 | if (strict) { |
||
314 | return false; |
||
315 | } |
||
316 | return true; |
||
317 | } |
||
318 | |||
319 | //decimal counter |
||
320 | function decimalCounter(_a, _b) { |
||
321 | _a = removeTrailingZeroes(_a); |
||
322 | _b = removeTrailingZeroes(_b); |
||
323 | var a = _a.split("."); |
||
324 | var b = _b.split("."); |
||
325 | //if zero no decimal movement is needed, add ".0" to the back |
||
326 | var decimalCount = 0; |
||
327 | if (a[1] !== undefined) { |
||
328 | if (a[1] === "0" && a[1].length === 1) { |
||
329 | decimalCount += 0; |
||
330 | } else { |
||
331 | decimalCount += a[1].length; |
||
332 | } |
||
333 | } |
||
334 | |||
335 | if (b[1] !== undefined) { |
||
336 | if (b[1] === "0" && b[1].length === 1) { |
||
337 | decimalCount += 0; |
||
338 | } else { |
||
339 | decimalCount += b[1].length; |
||
340 | } |
||
341 | } |
||
342 | |||
343 | return decimalCount; |
||
344 | } |
||
345 | |||
346 | //fix Clean for addition |
||
347 | function fixAdd(a, b) { |
||
348 | var t; |
||
349 | |||
350 | a = signFix(a); |
||
351 | b = signFix(b); |
||
352 | |||
353 | var signa = getSign(a); |
||
354 | var signb = getSign(b); |
||
355 | |||
356 | a = abs(a); |
||
357 | b = abs(b); |
||
358 | |||
359 | t = pairDecimal(a, b); |
||
360 | a = t[0]; |
||
361 | b = t[1]; |
||
362 | |||
363 | a = removeLeadingZeroes(a); |
||
364 | b = removeLeadingZeroes(b); |
||
365 | |||
366 | return [a, b, signa, signb]; |
||
367 | } |
||
368 | |||
369 | //check fix for mult |
||
370 | function fixMult(a, b) { |
||
371 | var d, signa, signb; |
||
372 | |||
373 | a = signFix(a); |
||
374 | b = signFix(b); |
||
375 | |||
376 | signa = getSign(a); |
||
377 | signb = getSign(b); |
||
378 | |||
379 | a = abs(a); |
||
380 | b = abs(b); |
||
381 | |||
382 | a = (a.indexOf(".") > -1) ? removeTrailingZeroes(a) : a; |
||
383 | b = (b.indexOf(".") > -1) ? removeTrailingZeroes(b) : b; |
||
384 | |||
385 | d = decimalCounter(a, b); |
||
386 | |||
387 | a = (a.slice(-2) === ".0") ? a.slice(0, a.length - 2) : a; |
||
388 | b = (b.slice(-2) === ".0") ? b.slice(0, b.length - 2) : b; |
||
389 | |||
390 | a = removeLeadingZeroes(a); |
||
391 | b = removeLeadingZeroes(b); |
||
392 | |||
393 | return [a, b, signa, signb, d]; |
||
394 | |||
395 | } |
||
396 | |||
397 | //a adds the integer part of a string |
||
398 | View Code Duplication | function a(_a, _b) { |
|
399 | var al, bl, i; |
||
400 | //_a and _b are strings |
||
401 | //made of pure integers |
||
402 | al = _a.length; |
||
403 | bl = _b.length; |
||
404 | if (al > bl) { |
||
405 | _b = zero(al - bl) + _b; |
||
406 | } else if (al < bl) { |
||
407 | _a = zero(bl - al) + _a; |
||
408 | } |
||
409 | _a = _a.split("").reverse(); |
||
410 | _b = _b.split("").reverse(); |
||
411 | |||
412 | var carry = 0, |
||
413 | sum = [], |
||
414 | temp = 0; |
||
415 | al = _a.length; |
||
416 | for (i = 0; i < al; i++) { |
||
417 | temp = _a[i] * 1 + _b[i] * 1 + carry; |
||
418 | if (temp >= 10) { |
||
419 | temp -= 10; |
||
420 | carry = 1; |
||
421 | } else { |
||
422 | carry = 0; |
||
423 | } |
||
424 | sum.push(temp + ""); |
||
425 | } |
||
426 | if (carry > 0) { |
||
427 | sum.push("1"); |
||
428 | } |
||
429 | return sum.reverse().join(""); |
||
430 | } |
||
431 | |||
432 | //ad adds the decimal part of string |
||
433 | View Code Duplication | function ad(_a, _b) { |
|
434 | var al, bl, i; |
||
435 | //_a and _b are are strings |
||
436 | //made of pure integers |
||
437 | al = _a.length; |
||
438 | bl = _b.length; |
||
439 | if (al > bl) { |
||
440 | _b += zero(al - bl); |
||
441 | } else if (al < bl) { |
||
442 | _a += zero(bl - al); |
||
443 | } |
||
444 | _a = _a.split("").reverse(); |
||
445 | _b = _b.split("").reverse(); |
||
446 | |||
447 | var carry = 0, |
||
448 | sum = [], |
||
449 | temp = 0; |
||
450 | al = _a.length; |
||
451 | for (i = 0; i < al; i++) { |
||
452 | temp = _a[i] * 1 + _b[i] * 1 + carry; |
||
453 | if (temp >= 10) { |
||
454 | temp -= 10; |
||
455 | carry = 1; |
||
456 | } else { |
||
457 | carry = 0; |
||
458 | } |
||
459 | sum.push(temp + ""); |
||
460 | } |
||
461 | return [sum.reverse().join(""), carry + ""]; |
||
462 | } |
||
463 | |||
464 | //add is a operator |
||
465 | function ADD(_a, _b) { |
||
466 | var aa = _a.split("."); |
||
467 | var bb = _b.split("."); |
||
468 | //now add integer part only |
||
469 | var integerPart = a(aa[0], bb[0]); |
||
470 | //then add decimal part only |
||
471 | var decimalPart = ad(aa[1], bb[1]); |
||
472 | //then join the results |
||
473 | return removeLeadingZeroes(a(integerPart, decimalPart[1])) + "." + removeTrailingZeroes(decimalPart[0]); |
||
474 | } |
||
475 | |||
476 | //s subtracts the integer part of a string |
||
477 | View Code Duplication | function s(x, y) { |
|
478 | var xl, yl, i; |
||
479 | xl = x.length; |
||
480 | yl = y.length; |
||
481 | if (xl < yl) { |
||
482 | x = zero(yl - xl) + x; |
||
483 | } else if (xl > yl) { |
||
484 | y = zero(xl - yl) + y; |
||
485 | } |
||
486 | x = x.split("").reverse(); |
||
487 | y = y.split("").reverse(); |
||
488 | |||
489 | var borrow = 0, |
||
490 | diff = [], |
||
491 | temp = 0; |
||
492 | xl = x.length; |
||
493 | for (i = 0; i < xl; i++) { |
||
494 | temp = (x[i] * 1) - (y[i] * 1) - borrow; |
||
495 | if (temp < 0) { |
||
496 | temp = (10 + x[i] * 1 - y[i] * 1 - borrow); |
||
497 | borrow = 1; |
||
498 | } else { |
||
499 | borrow = 0; |
||
500 | } |
||
501 | diff.push(temp); |
||
502 | } |
||
503 | return diff.reverse().join(""); |
||
504 | } |
||
505 | |||
506 | //sd subtracts the decimal part of string |
||
507 | View Code Duplication | function sd(x, y) { |
|
508 | var xl, yl, i; |
||
509 | xl = x.length; |
||
510 | yl = y.length; |
||
511 | if (xl < yl) { |
||
512 | x += zero(yl - xl); |
||
513 | } else if (xl > yl) { |
||
514 | y += zero(xl - yl); |
||
515 | } |
||
516 | |||
517 | x = x.split("").reverse(); |
||
518 | y = y.split("").reverse(); |
||
519 | |||
520 | var borrow = 0, |
||
521 | diff = [], |
||
522 | temp = 0; |
||
523 | xl = x.length; |
||
524 | for (i = 0; i < xl; i++) { |
||
525 | temp = (x[i] * 1) - (y[i] * 1) - borrow; |
||
526 | if (temp < 0) { |
||
527 | temp = (10 + x[i] * 1 - y[i] * 1 - borrow); |
||
528 | borrow = 1; |
||
529 | } else { |
||
530 | borrow = 0; |
||
531 | } |
||
532 | diff.push(temp); |
||
533 | } |
||
534 | return [diff.reverse().join(""), borrow + ""]; |
||
535 | } |
||
536 | |||
537 | //subtract is a operator |
||
538 | function subtract(_a, _b) { |
||
539 | var aa = _a.split("."); |
||
540 | var bb = _b.split("."); |
||
541 | //now subtract integer part only |
||
542 | var integerPart = s(aa[0], bb[0]); |
||
543 | //then subtract decimal part only |
||
544 | var decimalPart = sd(aa[1], bb[1]); |
||
545 | //then join the results |
||
546 | return removeLeadingZeroes(s(integerPart, decimalPart[1])) + "." + removeTrailingZeroes(decimalPart[0]); |
||
547 | } |
||
548 | |||
549 | //decider |
||
550 | function add(_a, _b) { |
||
551 | var t = fixAdd(_a, _b); |
||
552 | var signa = t[2]; |
||
553 | var signb = t[3]; |
||
554 | var aa = t[0]; |
||
555 | var bb = t[1]; |
||
556 | var first = max(aa, bb); |
||
557 | var second = min(aa, bb); |
||
558 | var output = "0.0"; |
||
559 | if (getSign(_a) === getSign(_b)) { |
||
560 | return signa + ADD(aa, bb); |
||
561 | } |
||
562 | if (aa === first && bb === first) { |
||
563 | //different sign same numbers? Its zero! |
||
564 | return "0.0"; |
||
565 | } else if (aa === first) { |
||
566 | return signa + subtract(first, second); |
||
567 | } else if (bb === first) { |
||
568 | return signb + subtract(first, second); |
||
569 | } |
||
570 | |||
571 | return output; |
||
572 | } |
||
573 | |||
574 | //multiply large integer by a single digit |
||
575 | function m(_a, _b) { |
||
576 | _a = _a.split("").reverse().join(""); |
||
577 | var i, temp, carry = 0, |
||
578 | output = ""; |
||
579 | var al = _a.length; |
||
580 | for (i = 0; i < al; i++) { |
||
581 | temp = (1 * _a[i]) * (1 * _b) + carry; |
||
582 | output += (temp % 10); |
||
583 | carry = Math.floor(temp / 10); |
||
584 | } |
||
585 | if (carry) { |
||
586 | return carry + output.split("").reverse().join(""); |
||
587 | } |
||
588 | return output.split("").reverse().join(""); |
||
589 | } |
||
590 | |||
591 | //multiplication operator |
||
592 | function multiply(_a, _b) { |
||
593 | var t = fixMult(_a, _b); |
||
594 | var sign = "-"; |
||
595 | var decimal = t[4]; |
||
596 | if (t[2] === t[3]) { |
||
597 | sign = ""; |
||
598 | } |
||
599 | var aa = removeDecimal(t[0]); |
||
600 | var bb = removeDecimal(t[1].split("").reverse().join("")); |
||
601 | |||
602 | if (aa === "0" || bb === "0") { |
||
603 | return "0.0"; |
||
604 | } else if (aa === "1") { |
||
605 | return sign + t[1]; |
||
606 | } else if (bb === "1") { |
||
607 | return sign + t[0]; |
||
608 | } |
||
609 | |||
610 | var i, output = "0", |
||
611 | tmp; |
||
612 | var bbl = bb.length; |
||
613 | for (i = 0; i < bbl; i++) { |
||
614 | tmp = m(aa, bb[i]); |
||
615 | //console.log(tmp); |
||
616 | output = a(output, tmp + zero(i)); |
||
617 | //console.log(output); |
||
618 | } |
||
619 | //console.log(decimal); |
||
620 | if (decimal === 0) { |
||
621 | return sign + removeLeadingZeroes(output + ".0"); |
||
622 | } |
||
623 | var d = (output.length) - decimal; |
||
624 | return sign + removeLeadingZeroes(splice(output, d, 0, ".")); |
||
625 | } |
||
626 | |||
627 | //integer if decimal is zero |
||
628 | function turnToIntIfDecIsZero(str) { |
||
629 | var a = str.split("."); |
||
630 | if (a[1] === "0" && a[1].length === 1) { |
||
631 | return a[0]; |
||
632 | } |
||
633 | return str; |
||
634 | } |
||
635 | |||
636 | //forrmats two numbers and returns an array |
||
637 | function pair(a, b, skip) { |
||
638 | a = a.split("."); |
||
639 | b = b.split("."); |
||
640 | |||
641 | a[1] = (a.length - 1) === 1 ? a[1] : "0"; |
||
642 | b[1] = (b.length - 1) === 1 ? b[1] : "0"; |
||
643 | |||
644 | if (!skip) { |
||
645 | var ala = a[0].length; |
||
646 | var alb = b[0].length; |
||
647 | var bla = a[1].length; |
||
648 | var blb = b[1].length; |
||
649 | |||
650 | while (ala > alb) { |
||
651 | b[0] = "0" + b[0]; |
||
652 | alb += 1; |
||
653 | } |
||
654 | while (ala < alb) { |
||
655 | a[0] = "0" + a[0]; |
||
656 | ala += 1; |
||
657 | } |
||
658 | |||
659 | while (bla > blb) { |
||
660 | b[1] += "0"; |
||
661 | blb += 1; |
||
662 | } |
||
663 | while (bla < blb) { |
||
664 | a[1] += "0"; |
||
665 | bla += 1; |
||
666 | } |
||
667 | } |
||
668 | |||
669 | return [a.join("."), b.join(".")]; |
||
670 | } |
||
671 | |||
672 | //move decimal for division |
||
673 | function moveDecimal(_a, _b) { |
||
674 | _a = removeTrailingZeroes(_a); |
||
675 | _b = removeTrailingZeroes(_b); |
||
676 | var decimal = 0; |
||
677 | //var oldIndex = -1; |
||
678 | //var newIndex = -1; |
||
679 | var temp = pair(_a, _b); |
||
680 | //_b is the divisor! |
||
681 | _a = removeLeadingZeroes(temp[0]); |
||
682 | _b = removeTrailingZeroes(removeLeadingZeroes(temp[1])); |
||
683 | decimal = decimalCounter(_b, "0"); |
||
684 | |||
685 | //remove decimal of b |
||
686 | _b = removeDecimal(turnToIntIfDecIsZero(_b)); |
||
687 | //get the current position |
||
688 | //of the decimal |
||
689 | //oldIndex = _a.indexOf("."); |
||
690 | //remove the decimal |
||
691 | //_a = removeDecimal(_a); |
||
692 | //calculate the new pos |
||
693 | //newIndex = oldIndex + decimal; |
||
694 | //put into place |
||
695 | _a = splice(removeDecimal(_a), _a.indexOf(".") + decimal, 0, "."); |
||
696 | //finish |
||
697 | if (_a[_a.length - 1] === ".") { |
||
698 | _a += "0"; |
||
699 | } |
||
700 | _b += ".0"; |
||
701 | return [_a, _b]; |
||
702 | |||
703 | } |
||
704 | |||
705 | //functio divFix |
||
706 | function divFix(_a, _b) { |
||
707 | var d, signa, signb; |
||
708 | |||
709 | _a = signFix(_a); |
||
710 | _b = signFix(_b); |
||
711 | |||
712 | signa = getSign(_a); |
||
713 | signb = getSign(_b); |
||
714 | |||
715 | _a = abs(_a); |
||
716 | _b = abs(_b); |
||
717 | |||
718 | _a = (_a.indexOf(".") > -1) ? removeTrailingZeroes(_a) : _a + ".0"; |
||
719 | _b = (_b.indexOf(".") > -1) ? removeTrailingZeroes(_b) : _b + ".0"; |
||
720 | |||
721 | d = moveDecimal(_a, _b); |
||
722 | |||
723 | _a = d[0]; |
||
724 | _b = d[1]; |
||
725 | |||
726 | _b = (_b.slice(-2) === ".0") ? _b.slice(0, _b.length - 2) : _b; |
||
727 | |||
728 | _a = removeLeadingZeroes(_a); |
||
729 | _b = removeLeadingZeroes(_b); |
||
730 | |||
731 | return [_a, _b, signa, signb]; |
||
732 | } |
||
733 | |||
734 | //division for integers |
||
735 | function division(A, B, t) { |
||
736 | var OA = A; |
||
737 | if (A.length < B.length) { |
||
738 | return ["0", A]; |
||
739 | } else if (B === "0") { |
||
740 | throw "error!"; |
||
741 | } |
||
742 | |||
743 | var i, q, d, z = A.length - B.length; |
||
744 | var stack = []; |
||
745 | var abl = A.length - B.length + 1; |
||
746 | |||
747 | for (i = 0; i < abl; i++) { |
||
748 | d = B + zero(z); |
||
749 | //happens if there is a rmainder eg 956 / 34 |
||
750 | if (A.length > d.length) { |
||
751 | q = Math.floor(((A[0] + A[1]) * 1) / (d[0] * 1)) + ""; |
||
752 | //happens if we find the real remainder R |
||
753 | } else if (A.length < d.length && i === (abl - 1)) { |
||
754 | q = "0"; |
||
755 | stack.push(q); |
||
756 | break; |
||
757 | //happens if the next digit zero ex: 024 / 100 |
||
758 | } else if (A.length < d.length) { |
||
759 | q = "0"; |
||
760 | stack.push(q); |
||
761 | z = z - 1; |
||
762 | continue; |
||
763 | //happens if length of A === length of D |
||
764 | } else { |
||
765 | q = Math.floor((A[0] * 1) / (d[0] * 1)) + ""; |
||
766 | } |
||
767 | if (q.length >= 2) { |
||
768 | q = "9"; |
||
769 | } |
||
770 | var temp = m(d, q); |
||
771 | while (isMin(A, temp, true)) { |
||
772 | q = s(q, "1"); |
||
773 | temp = m(d, q); |
||
774 | } |
||
775 | stack.push(q); |
||
776 | A = removeLeadingZeroes(s(A, m(d, q))); |
||
777 | z = z - 1; |
||
778 | } |
||
779 | if (t) { |
||
780 | return [zero(OA.length - stack.length) + stack.join(""), A]; |
||
781 | } |
||
782 | return [removeLeadingZeroes(stack.join("")), floor(removeLeadingZeroes(A + ".0"))]; |
||
783 | } |
||
784 | |||
785 | //function count zeroes |
||
786 | function countZeroes(n) { |
||
787 | var i, nl = n.length; |
||
788 | var output = 0; |
||
789 | for (i = 0; i < nl; i++) { |
||
790 | if (n[i] === "0") { |
||
791 | output += 1; |
||
792 | } else { |
||
793 | break; |
||
794 | } |
||
795 | } |
||
796 | return output; |
||
797 | } |
||
798 | |||
799 | //function division |
||
800 | function Division(_a, _b) { |
||
801 | var temp = divFix(_a, _b); |
||
802 | var A = temp[0]; |
||
803 | var B = temp[1]; |
||
804 | var signA = temp[2]; |
||
805 | var signB = temp[3]; |
||
806 | var output = ""; |
||
807 | var sign = "-"; |
||
808 | var tmp = ""; |
||
809 | var t = ""; |
||
810 | //var i = 0, h = 0; |
||
811 | var r = ""; |
||
812 | var cz = 0; |
||
813 | var rl = 0; |
||
814 | |||
815 | if (signA === signB) { |
||
816 | sign = ""; |
||
817 | } |
||
818 | |||
819 | if (B === "0") { |
||
820 | throw "Division Error: Cannot Divide By Zero"; |
||
821 | } else if (A === "0.0") { |
||
822 | return "0.0"; |
||
823 | } |
||
824 | |||
825 | A = A.split("."); |
||
826 | tmp = division(A[0], B); |
||
827 | output += (tmp[0] + "."); |
||
828 | r = tmp[1]; |
||
829 | rl = r.length; |
||
830 | if (r === "0") { |
||
831 | r = ""; |
||
832 | rl = 0; |
||
833 | cz = countZeroes(A[1]); |
||
834 | if (A[1] === "0") { |
||
835 | cz = 0; |
||
836 | } |
||
837 | A[1] = removeLeadingZeroes(A[1]); |
||
838 | } |
||
839 | |||
840 | t = r + A[1]; |
||
841 | tmp = division(t + zero(limits.decimalDigits - 1), B, true); |
||
842 | output += (zero(cz) + tmp[0].substr(rl, tmp[0].length)); |
||
843 | |||
844 | return sign + removeTrailingZeroes(output); |
||
845 | } |
||
846 | |||
847 | //function power |
||
848 | function power(_a, _b) { |
||
849 | //patched for stopping big power |
||
850 | _b = abs(floor(_b)); |
||
851 | var i = "1"; |
||
852 | var output = _a; |
||
853 | while (isMin(i, _b, true)) { |
||
854 | //this should be slow, but checking for growing digits is adviced |
||
855 | if (abs(floor(output)).length > limits.decimalDigits) { |
||
856 | console.error("Error: Value of the exponent is larger than the predefined limit."); |
||
857 | console.error("Stopped at iteration " + i + " of " + _b + ". Errors may happen due to precision."); |
||
858 | output = output.substr(0, limits.decimalDigits) + zero(output.length - 1 - limits.decimalDigits); |
||
859 | return output; |
||
860 | } |
||
861 | i = a(i, "1"); |
||
862 | output = multiply(output, _a); |
||
863 | } |
||
864 | return output; |
||
865 | } |
||
866 | |||
867 | //compare numbers |
||
868 | function equalTo(_a, _b) { |
||
869 | //this is automatic |
||
870 | if (_a === _b) { |
||
871 | return true; |
||
872 | } |
||
873 | var arr = fixAdd(_a, _b); |
||
874 | if (arr[0] === arr[1] && arr[0] === "0.0") { |
||
875 | return true; |
||
876 | } |
||
877 | if (arr[0] === arr[1] && arr[2] === arr[3]) { |
||
878 | return true; |
||
879 | } |
||
880 | return false; |
||
881 | } |
||
882 | |||
883 | function maxTo(_a, _b) { |
||
884 | var arr = fixAdd(_a, _b); |
||
885 | if (arr[0] === arr[1] && arr[0] === "0.0") { |
||
886 | return false; |
||
887 | } |
||
888 | var bool = isMax(arr[2] + arr[0], arr[3] + arr[1], true); |
||
889 | return bool; |
||
890 | } |
||
891 | |||
892 | function minTo(_a, _b) { |
||
893 | var arr = fixAdd(_a, _b); |
||
894 | if (arr[0] === arr[1] && arr[0] === "0.0") { |
||
895 | return false; |
||
896 | } |
||
897 | var bool = isMin(arr[2] + arr[0], arr[3] + arr[1], true); |
||
898 | return bool; |
||
899 | } |
||
900 | |||
901 | function maxEqTo(_a, _b) { |
||
902 | var arr = fixAdd(_a, _b); |
||
903 | if (arr[0] === arr[1] && arr[0] === "0.0") { |
||
904 | return false; |
||
905 | } |
||
906 | var bool = isMax(arr[2] + arr[0], arr[3] + arr[1]); |
||
907 | return bool; |
||
908 | } |
||
909 | |||
910 | function minEqTo(_a, _b) { |
||
911 | var arr = fixAdd(_a, _b); |
||
912 | if (arr[0] === arr[1] && arr[0] === "0.0") { |
||
913 | return false; |
||
914 | } |
||
915 | var bool = isMin(arr[2] + arr[0], arr[3] + arr[1]); |
||
916 | return bool; |
||
917 | } |
||
918 | |||
919 | //create our function constructor for arb |
||
920 | //arb stands for arbitrary |
||
921 | function Arbshell(n) { |
||
922 | //n is string here |
||
923 | //serialize n |
||
924 | this.value = n.replace(/(\t|\s|[a-zA-Z])/g, ""); |
||
925 | } |
||
926 | Arbshell.prototype = { |
||
927 | rawAdd: function (n) { |
||
928 | var temp = this.value; |
||
929 | this.value = ADD(temp, n); |
||
930 | return this; |
||
931 | }, |
||
932 | rawSubtract: function (n) { |
||
933 | var temp = this.value; |
||
934 | this.value = subtract(temp, n); |
||
935 | return this; |
||
936 | }, |
||
937 | rawDivide: function (n) { |
||
938 | var temp = floor(this.value); |
||
939 | this.value = division(temp, floor(n))[0]; |
||
940 | return this; |
||
941 | }, |
||
942 | add: function (n) { |
||
943 | var temp = this.value; |
||
944 | this.value = add(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
945 | return this; |
||
946 | }, |
||
947 | sub: function (n) { |
||
948 | var temp = this.value; |
||
949 | this.value = add(temp, "-" + n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
950 | return this; |
||
951 | }, |
||
952 | mul: function (n) { |
||
953 | var temp = this.value; |
||
954 | this.value = multiply(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
955 | return this; |
||
956 | }, |
||
957 | div: function (n, o) { |
||
958 | if(typeof o === "number"){ |
||
959 | if(o > 0){ |
||
960 | limits.decimalDigits = o; |
||
961 | } |
||
962 | } |
||
963 | var temp = this.value; |
||
964 | this.value = Division(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
965 | limits.decimalDigits = 200; |
||
966 | return this; |
||
967 | }, |
||
968 | pow: function (n,o) { |
||
969 | if(typeof o === "number"){ |
||
970 | if(o > 0){ |
||
971 | limits.decimalDigits = o; |
||
972 | } |
||
973 | } |
||
974 | var temp = this.value; |
||
975 | this.value = power(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
976 | limits.decimalDigits = 200; |
||
977 | return this; |
||
978 | }, |
||
979 | eq: function (n) { |
||
980 | var temp = this.value; |
||
981 | return equalTo(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
982 | }, |
||
983 | lt: function (n) { |
||
984 | var temp = this.value; |
||
985 | return minTo(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
986 | }, |
||
987 | gt: function (n) { |
||
988 | var temp = this.value; |
||
989 | return maxTo(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
990 | }, |
||
991 | lte: function (n) { |
||
992 | var temp = this.value; |
||
993 | return minEqTo(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
994 | }, |
||
995 | gte: function (n) { |
||
996 | var temp = this.value; |
||
997 | return maxEqTo(temp, n.replace(/(\t|\s|[a-zA-Z])/g, "")); |
||
998 | }, |
||
999 | floor: function () { |
||
1000 | var temp = this.value; |
||
1001 | this.value = floor(temp); |
||
1002 | return this; |
||
1003 | }, |
||
1004 | abs: function () { |
||
1005 | var temp = this.value; |
||
1006 | this.value = abs(temp); |
||
1007 | return this; |
||
1008 | } |
||
1009 | }; |
||
1010 | |||
1011 | //export our object |
||
1012 | /** global: define */ |
||
1013 | if (typeof define === 'function' && define.amd) { |
||
1014 | define([], function (n) { |
||
1015 | return (new Arbshell(n)); |
||
1016 | }); |
||
1017 | } else if (typeof exports === 'object') { |
||
1018 | module.exports = function (n) { |
||
1019 | return (new Arbshell(n)); |
||
1020 | }; |
||
1021 | } else { |
||
1022 | scope.arb = function (n) { |
||
1023 | return (new Arbshell(n)); |
||
1024 | }; |
||
1025 | } |
||
1026 | })(this); |