Passed
Push — master ( 01a225...a0e189 )
by Miles MJ
01:38
created

arb.js ➔ minNeg   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1018
    define([], function (n) {
1019
      return (new Arbshell(n));
1020
    });
1021
  } else if (typeof exports === 'object') {
1022
    module.exports = function (n) {
1023
      return (new Arbshell(n));
1024
    };
1025
  } else {
1026
    scope.arb = function (n) {
1027
      return (new Arbshell(n));
1028
    };
1029
  }
1030
})(this);