devongovett/coffeepack — Issue #4: Please respect the license
Author: edvakf
Created: 2011-10-25 23:32:45+00:00
State: open

msgpack.js by @uupaa https://github.com/uupaa/msgpack.js/blob/da6d906ce2241c41198a0c122ab24cfc6f09d319/msgpack.js

```JavaScript
} else { // double
// THX!! @edvakf
// http://javascript.g.hatena.ne.jp/edvakf/20101128/1291000731
sign = mix < 0;
sign && (mix *= -1);

// add offset 1023 to ensure positive
// 0.6931471805599453 = Math.LN2;
exp = ((Math.log(mix) / 0.6931471805599453) + 1023) | 0;

// shift 52 - (exp - 1023) bits to make integer part exactly 53 bits,
// then throw away trash less than decimal point
frac = mix * Math.pow(2, 52 + 1023 - exp);

// S+-Exp(11)--++-----------------Fraction(52bits)-----------------------+
// || || |
// v+----------++--------------------------------------------------------+
// 00000000|00000000|00000000|00000000|00000000|00000000|00000000|00000000
// 6 5 55 4 4 3 2 1 8 0
// 3 6 21 8 0 2 4 6
//
// +----------high(32bits)-----------+ +----------low(32bits)------------+
// | | | |
// +---------------------------------+ +---------------------------------+
// 3 2 21 1 8 0
// 1 4 09 6
low = frac & 0xffffffff;
sign && (exp |= 0x800);
high = ((frac / 0x100000000) & 0xfffff) | (exp << 20);

rv.push(0xcb, (high >> 24) & 0xff, (high >> 16) & 0xff,
(high >> 8) & 0xff, high & 0xff,
(low >> 24) & 0xff, (low >> 16) & 0xff,
(low >> 8) & 0xff, low & 0xff);
}
```

Your code https://github.com/devongovett/coffeepack/blob/6fc994db1cf4f7850f62d2d12bf9755e062bbad0/msgpack.coffee

```CoffeeScript
# float
else
# TODO: encode single precision if possible
sign = val < 0
val *= -1 if sign

# add offset 1023 to ensure positive
exp = ((Math.log(val) / Math.LN2) + 1023) | 0

# shift 52 - (exp - 1023) bits to make integer part exactly 53 bits,
# then throw away trash less than decimal point
frac = val * Math.pow(2, 52 + 1023 - exp)

low = frac & 0xffffffff
exp |= 0x800 if sign
high = ((frac / 0x100000000) & 0xfffff) | (exp << 20)

bytes.push 0xcb, (high >> 24) & 0xff, (high >> 16) & 0xff,
(high >> 8) & 0xff, high & 0xff,
(low >> 24) & 0xff, (low >> 16) & 0xff,
(low >> 8) & 0xff, low & 0xff
```

This is the part I wrote for @uupaa so I own the copyright, and @uupaa does also own the copyright for his code. Please respect the original authors and the license.