-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
132 lines (100 loc) · 3.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
;(function (global) {
'use strict';
var liberate = Function.prototype.bind.bind(Function.prototype.call),
reduce = liberate(Array.prototype.reduce);
// Computes the multiplier necessary to make x >= 1,
// effectively eliminating miscalculations caused by
// finite precision.
function multiplier(x) {
var converted = x.toFixed && x < 1e-6 ? x.toFixed(15).replace(/(\..*?)(0+)$/, "$1") : x.toString()
var parts = converted.split('.');
if (parts.length < 2) {
return 1;
}
return Math.pow(10, parts[1].length);
}
// Given a variable number of arguments, returns the maximum
// multiplier that must be used to normalize an operation involving
// all of them.
function correctionFactor() {
return reduce(arguments, function (prev, next) {
next = multiplier(next);
return prev > next ? prev : next;
}, -Infinity);
}
// Returns the "corrected" integer value of a
// floating point multiplication operation
function correctedValue(val, corrFactor) {
return (val * corrFactor).toFixed(0) - 0;
}
// If an array is passed as the first argument to the
// mathematical operation functions, reduce the array
// using the passed in mathematical function
function reduceArray(arr, func) {
return arr.reduce(function (previousValue, currentValue, index, array) {
return func(previousValue, currentValue);
});
}
// Math Operations
var sinfulMath = {
add: function () {
if (Array.isArray(arguments[0])) {
return reduceArray(arguments[0], sinfulMath.add);
}
var corrFactor = correctionFactor.apply(null, arguments);
function cback(accum, curr, currI, O) {
return accum + correctedValue(curr, corrFactor);
}
return reduce(arguments, cback, 0) / corrFactor;
},
sub: function () {
if (Array.isArray(arguments[0])) {
return reduceArray(arguments[0], sinfulMath.sub);
}
var corrFactor = correctionFactor.apply(null, arguments),
first = arguments[0];
function cback(accum, curr, currI, O) {
return accum - correctedValue(curr, corrFactor);
}
delete arguments[0];
return reduce(arguments, cback, correctedValue(first, corrFactor)) / corrFactor;
},
mul: function () {
if (Array.isArray(arguments[0])) {
return reduceArray(arguments[0], sinfulMath.mul);
}
function cback(accum, curr, currI, O) {
var corrFactor = correctionFactor(accum, curr);
return correctedValue(accum, corrFactor) * correctedValue(curr, corrFactor) / (corrFactor * corrFactor);
}
return reduce(arguments, cback, 1);
},
div: function () {
if (Array.isArray(arguments[0])) {
return reduceArray(arguments[0], sinfulMath.div);
}
function cback(accum, curr, currI, O) {
var corrFactor = correctionFactor(accum, curr);
return correctedValue(accum, corrFactor) / correctedValue(curr, corrFactor);
}
return reduce(arguments, cback);
}
};
sinfulMath['subtract'] = sinfulMath['sub'];
sinfulMath['multiply'] = sinfulMath['mul'];
sinfulMath['divide'] = sinfulMath['div'];
// Node/CommonJS
if (typeof module !== 'undefined' && module.exports) {
module.exports = sinfulMath;
}
// AMD
else if (typeof define === 'function' && define.amd) {
define(function () {
return sinfulMath;
});
}
// Browser
else {
global['sinfulMath'] = sinfulMath;
}
})(this);