| 1 | JavaScript Numbers are represented as [IEEE 754 double-precision floats](http://steve.hollasch.net/cgindex/coding/ieeefloat.html). Unfortunately, this means they lose integer precision for values beyond +/- 2^^53. For projects that need to accurately handle 64-bit ints, such as [node-thrift](https://github.com/wadey/node-thrift), a performant, Number-like class is needed. Int64 is that class.
|
|---|
| 2 |
|
|---|
| 3 | Int64 instances look and feel much like JS-native Numbers. By way of example ...
|
|---|
| 4 | ```js
|
|---|
| 5 | // First, let's illustrate the problem ...
|
|---|
| 6 | > (0x123456789).toString(16)
|
|---|
| 7 | '123456789' // <- what we expect.
|
|---|
| 8 | > (0x123456789abcdef0).toString(16)
|
|---|
| 9 | '123456789abcdf00' // <- Ugh! JS doesn't do big ints. :(
|
|---|
| 10 |
|
|---|
| 11 | // So let's create a couple Int64s using the above values ...
|
|---|
| 12 |
|
|---|
| 13 | // Require, of course
|
|---|
| 14 | > Int64 = require('node-int64')
|
|---|
| 15 |
|
|---|
| 16 | // x's value is what we expect (the decimal value of 0x123456789)
|
|---|
| 17 | > x = new Int64(0x123456789)
|
|---|
| 18 | [Int64 value:4886718345 octets:00 00 00 01 23 45 67 89]
|
|---|
| 19 |
|
|---|
| 20 | // y's value is Infinity because it's outside the range of integer
|
|---|
| 21 | // precision. But that's okay - it's still useful because it's internal
|
|---|
| 22 | // representation (octets) is what we passed in
|
|---|
| 23 | > y = new Int64('123456789abcdef0')
|
|---|
| 24 | [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
|
|---|
| 25 |
|
|---|
| 26 | // Let's do some math. Int64's behave like Numbers. (Sorry, Int64 isn't
|
|---|
| 27 | // for doing 64-bit integer arithmetic (yet) - it's just for carrying
|
|---|
| 28 | // around int64 values
|
|---|
| 29 | > x + 1
|
|---|
| 30 | 4886718346
|
|---|
| 31 | > y + 1
|
|---|
| 32 | Infinity
|
|---|
| 33 |
|
|---|
| 34 | // Int64 string operations ...
|
|---|
| 35 | > 'value: ' + x
|
|---|
| 36 | 'value: 4886718345'
|
|---|
| 37 | > 'value: ' + y
|
|---|
| 38 | 'value: Infinity'
|
|---|
| 39 | > x.toString(2)
|
|---|
| 40 | '100100011010001010110011110001001'
|
|---|
| 41 | > y.toString(2)
|
|---|
| 42 | 'Infinity'
|
|---|
| 43 |
|
|---|
| 44 | // Use JS's isFinite() method to see if the Int64 value is in the
|
|---|
| 45 | // integer-precise range of JS values
|
|---|
| 46 | > isFinite(x)
|
|---|
| 47 | true
|
|---|
| 48 | > isFinite(y)
|
|---|
| 49 | false
|
|---|
| 50 |
|
|---|
| 51 | // Get an octet string representation. (Yay, y is what we put in!)
|
|---|
| 52 | > x.toOctetString()
|
|---|
| 53 | '0000000123456789'
|
|---|
| 54 | > y.toOctetString()
|
|---|
| 55 | '123456789abcdef0'
|
|---|
| 56 |
|
|---|
| 57 | // Finally, some other ways to create Int64s ...
|
|---|
| 58 |
|
|---|
| 59 | // Pass hi/lo words
|
|---|
| 60 | > new Int64(0x12345678, 0x9abcdef0)
|
|---|
| 61 | [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
|
|---|
| 62 |
|
|---|
| 63 | // Pass a Buffer
|
|---|
| 64 | > new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]))
|
|---|
| 65 | [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
|
|---|
| 66 |
|
|---|
| 67 | // Pass a Buffer and offset
|
|---|
| 68 | > new Int64(new Buffer([0,0,0,0,0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]), 4)
|
|---|
| 69 | [Int64 value:Infinity octets:12 34 56 78 9a bc de f0]
|
|---|
| 70 |
|
|---|
| 71 | // Pull out into a buffer
|
|---|
| 72 | > new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer()
|
|---|
| 73 | <Buffer 12 34 56 78 9a bc de f0>
|
|---|
| 74 |
|
|---|
| 75 | // Or copy into an existing one (at an offset)
|
|---|
| 76 | > var buf = new Buffer(1024);
|
|---|
| 77 | > new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).copy(buf, 512);
|
|---|
| 78 | ```
|
|---|