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