1 // Credit: https://github.com/paulmillr/es6-shim/
5 var abs = Math.abs, floor = Math.floor, log = Math.log, min = Math.min
6 , pow = Math.pow, LN2 = Math.LN2
9 roundToEven = function (n) {
10 var w = floor(n), f = n - w;
11 if (f < 0.5) return w;
12 if (f > 0.5) return w + 1;
13 return w % 2 ? w + 1 : w;
16 module.exports = function (v, ebits, fbits) {
17 var bias = (1 << (ebits - 1)) - 1, s, e, f, i, bits, str, bytes;
19 // Compute sign, exponent, fraction
22 // http://dev.w3.org/2006/webapi/WebIDL/#es-type-mapping
24 f = pow(2, fbits - 1);
26 } else if (v === Infinity || v === -Infinity) {
33 s = (1 / v === -Infinity) ? 1 : 0;
38 if (v >= pow(2, 1 - bias)) {
39 e = min(floor(log(v) / LN2), 1023);
40 f = roundToEven(v / pow(2, e) * pow(2, fbits));
41 if (f / pow(2, fbits) >= 2) {
52 f = f - pow(2, fbits);
57 f = roundToEven(v / pow(2, 1 - bias - fbits));
61 // Pack sign, exponent, fraction
63 for (i = fbits; i; i -= 1) {
64 bits.push(f % 2 ? 1 : 0);
67 for (i = ebits; i; i -= 1) {
68 bits.push(e % 2 ? 1 : 0);
78 bytes.push(parseInt(str.substring(0, 8), 2));
79 str = str.substring(8);