-
Notifications
You must be signed in to change notification settings - Fork 0
/
csmath.js
313 lines (242 loc) · 8.43 KB
/
csmath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2012, Jens Nockert
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
void
function(global) {
"use strict";
var CSMath = {}
if (navigator.userAgent.indexOf("Chrome") != -1) {
CSMath.accurate = {
exp: true,
pow: true,
log: true,
sqrt: false
}
} else {
CSMath.accurate = {
exp: true,
pow: true,
log: true,
sqrt: true
}
}
CSMath.abs = Math.abs
CSMath.E = Math.E
CSMath.PI = Math.PI
CSMath.LN2 = Math.LN2
CSMath.LN10 = Math.LN10
CSMath.LOG2E = Math.LOG2E
CSMath.LOG10E = Math.LOG10E
CSMath.SQRT2 = Math.SQRT2
CSMath.SQRT1_2 = Math.SQRT1_2
/* TODO: All exponential functions have low precision */
CSMath.exp = Math.exp
CSMath.exp1m = function(a) {
return CSMath.exp(a - 1.0)
}
CSMath.pow = Math.pow
/*
* fma(a, b, c)
* - Calculates a * b + c with a single rounding, `a * b + c` in Javascript would otherwise round twice
*/
CSMath.fma = function(a, b, c) {
// Start veltkamp split macro
var aHigh = 134217729 * a,
aLow
aHigh = aHigh + (a - aHigh)
aLow = a - aHigh
// End veltkamp split macro
// Start veltkamp split macro
var bHigh = 134217729 * b,
bLow
bHigh = bHigh + (b - bHigh)
bLow = b - bHigh
// End veltkamp split macro
// Start two-prod macro
var r1 = a * b
var r2 = -r1 + aHigh * bHigh + aHigh * bLow + aLow * bHigh + aLow * bLow
// End two-prod macro
// Start two-sum macro
var s = r1 + c
var t = (r1 - (s - c)) + (c - (s - r1))
// End two-sum macro
return s + (t + r2)
}
/*
* fms(a, b, c)
* - Calculates a * b - c with a single rounding, `a * b - c` in Javascript would otherwise round twice
*/
CSMath.fms = function(a, b, c) {
// Start veltkamp split macro
var aHigh = 134217729 * a,
aLow
aHigh = aHigh + (a - aHigh)
aLow = a - aHigh
// End veltkamp split macro
// Start veltkamp split macro
var bHigh = 134217729 * b,
bLow
bHigh = bHigh + (b - bHigh)
bLow = b - bHigh
// End veltkamp split macro
// Start two-prod macro
var r1 = a * b
var r2 = -r1 + aHigh * bHigh + aHigh * bLow + aLow * bHigh + aLow * bLow
// End two-prod macro
// Start two-sum macro
var s = r1 + -c
var t = (r1 - (s - -c)) + (-c - (s - r1))
// End two-sum macro
return s + (t + r2)
}
/* TODO: The hyperbolic functions have low precision */
CSMath.sinh = function(a) {
return (CSMath.exp(a) - CSMath.exp(-a)) / 2.0
}
CSMath.cosh = function(a) {
return (CSMath.exp(a) + CSMath.exp(-a)) / 2.0
}
CSMath.tanh = function(a) {
return (CSMath.exp(a) - CSMath.exp(-a)) / (CSMath.exp(a) + CSMath.exp(-a))
}
CSMath.asinh = function(a) {
return CSMath.log(a + CSMath.sqrt(1.0 + a * a))
}
CSMath.acosh = function(a) {
return 2.0 * CSMath.log(CSMath.sqrt((a + 1.0) / 2.0) + CSMath.sqrt((a - 1.0) / 2.0))
}
CSMath.atanh = function(a) {
return (CSMath.log(1.0 + a) - CSMath.log(1.0 - a)) / 2.0
}
/* TODO: All logarithm functions have low precision */
CSMath.log = Math.log
CSMath.log2 = function(a) {
return CSMath.log(a) / CSMath.LN2
}
CSMath.log10 = function(a) {
return CSMath.log(a) / CSMath.LN10
}
CSMath.log1p = function(a) {
return CSMath.log(1.0 + a)
}
CSMath.norm2 = function() {
var list = Array.prototype.slice.call(arguments).sort(function(a, b) {
return Math.abs(b) - Math.abs(a)
})
var head = CSMath.abs(list[0]),
s = 1.0,
c = 0.0,
n = list.length
for (var i = 1; i < n; i++) {
var value = CSMath.abs(list[i]) / head
value = value * value
var y = c + value
var u = value - (y - c)
var t = s + y
var v = y - (t - s)
var z = u + v
s = t + z
c = z - (s - t)
}
return CSMath.sqrt(s * head)
}
CSMath.hypot2 = function() {
var list = Array.prototype.slice.call(arguments).sort(function(a, b) {
return Math.abs(b) - Math.abs(a)
})
var head = CSMath.abs(list[0]),
s = 1.0,
c = 0.0,
n = list.length
for (var i = 1; i < n; i++) {
var value = CSMath.abs(list[i]) / head
value = value * value
var y = c + value
var u = value - (y - c)
var t = s + y
var v = y - (t - s)
var z = u + v
s = t + z
c = z - (s - t)
}
return s * head
}
CSMath.min = Math.min
CSMath.max = Math.max
CSMath.hypot = CSMath.norm2
/* TODO: The trigonometric functions have low precision */
CSMath.round = Math.round
// CSMath.trunc = ?
CSMath.sign = function(a) {
return a / Math.abs(a)
}
if (CSMath.accurate['sqrt']) {
CSMath.sqrt = Math.sqrt
} else {
/*
* Algorithm from 'Divide, Square Root and Remainder Algorithms for the IA64
* Architecture' by Intel
*
* - It is proven correct under a few assumptions
* 1. That Math.sqrt is at least as accurate as the IA64 approximation
* instruction.
* 2. That we're using the IA64 extended floating point format. (Which
* we are not)
* - I haven't checked if this algorithm actually works in double-precision,
* but my intuition says that it should be fine.
*/
var sqrtApproximation = Math.sqrt
CSMath.sqrt = function(a) {
var y0 = 1.0 / sqrtApproximation(a)
var H0 = 0.5 * y0,
S0 = (a * y0)
var d0 = -CSMath.fms(S0, H0, 0.5)
var H1 = CSMath.fma(d0, H0, H0),
S1 = CSMath.fma(d0, S0, S0)
var d1 = -CSMath.fms(S1, H1, 0.5)
var H2 = CSMath.fma(d1, H1, H1),
S2 = CSMath.fma(d1, S1, S1)
var d2 = -CSMath.fms(S2, H2, 0.5),
e2 = -CSMath.fms(S2, S2, a)
var H3 = CSMath.fma(d2, H2, H2),
S3 = CSMath.fma(e2, H2, S2)
var e3 = -CSMath.fms(S3, S3, a)
return CSMath.fma(e3, H3, S3)
}
}
/* TODO: The root functions have low precision */
CSMath.cbrt = function(a) {
return Math.pow(a, 1.0 / 3.0)
}
/* TODO: The trigonometric functions have low precision */
CSMath.sin = Math.sin
CSMath.cos = Math.cos
CSMath.tan = Math.tan
CSMath.asin = Math.asin
CSMath.acos = Math.acos
CSMath.atan = Math.atan
CSMath.atan2 = Math.atan2
global.CSMath = CSMath
}(this || global)