node-int64
Advanced tools
Comparing version 0.1.0 to 0.2.0
103
Int64.js
/** | ||
* Support for handling 64-bit int numbers in Javascript (node.js) | ||
* | ||
* JS Numbers are IEEE-754 double-precision floats, which limits the range of | ||
* integer values that can be accurately represented to +/- 2^^53. | ||
* JS Numbers are IEEE-754 binary double-precision floats, which limits the | ||
* range of values that can be represented with integer precision to: | ||
* | ||
* 2^^53 <= N <= 2^53 | ||
* | ||
* Int64 objects wrap a node Buffer that holds the 8-bytes of int64 data. These | ||
* objects operate directly on the buffer, which means that if they are created | ||
* using an existing buffer, setting the value will modify the Buffer and | ||
* objects operate directly on the buffer which means that if they are created | ||
* using an existing buffer then setting the value will modify the Buffer, and | ||
* vice-versa. | ||
* | ||
* For details about IEEE-754 see: | ||
* http://en.wikipedia.org/wiki/Double_precision_floating-point_format | ||
*/ | ||
@@ -18,4 +23,6 @@ | ||
// Map for converting hex octets to strings | ||
var _HEX = []; | ||
for (var i = 0; i < 256; i++) _HEX[i] = (i > 0xF ? '' : '0') + i.toString(16); | ||
var _HEX = [], _PADHEX = []; | ||
for (var i = 0; i < 256; i++) { | ||
_HEX[i] = (i > 0xF ? '' : '0') + i.toString(16); | ||
} | ||
@@ -68,5 +75,5 @@ // | ||
* Set the value: | ||
* new Int64(string) - A hexidecimal string | ||
* new Int64(number) - Number (throws if n is outside int64 range) | ||
* new Int64(hi, lo) - Raw bits as two 32-bit values | ||
* setValue(string) - A hexidecimal string | ||
* setValue(number) - Number (throws if n is outside int64 range) | ||
* setValue(hi, lo) - Raw bits as two 32-bit values | ||
*/ | ||
@@ -96,7 +103,7 @@ setValue: function(hi, lo) { | ||
// TODO: Do we want to throw if hi/lo is outside int32 range here? | ||
// Technically we should throw if hi/lo is outside int32 range here, but | ||
// it's not worth the effort. | ||
// Copy bytes to buffer | ||
b = this.buffer; | ||
var o = this.offset; | ||
var b = this.buffer, o = this.offset; | ||
for (var i = 7; i >= 0; i--) { | ||
@@ -112,47 +119,53 @@ b[o+i] = lo & 0xff; | ||
/** | ||
* Return the approximate error involved in converting the current value to a | ||
* native JS number. If > 0, the value is outside the range JS can represent | ||
* to integer precision. | ||
* Convert to a JS Number. Returns +/-Infinity for values that can't be | ||
* represented to integer precision. | ||
*/ | ||
error: function() { | ||
return Math.ceil(Math.abs(this.valueOf()) / Int64.MAX_INT) - 1; | ||
valueOf: function() { | ||
var b = this.buffer, o = this.offset; | ||
var negate = b[0] & 0x80, x = 0, carry = 1; | ||
for (var i = 0, ii = o + 7; i < 8; i++, ii--) { | ||
var v = b[ii]; | ||
// Do a running 2's complement for negative numbers | ||
if (negate) { | ||
v = (v ^ 0xff) + carry; | ||
carry = v >> 8; | ||
} | ||
x += (v & 0xff) * Math.pow(256, i); | ||
} | ||
// Return Infinity if we've lost integer precision | ||
if (x >= Int64.MAX_INT) { | ||
return negate ? -Infinity : Infinity; | ||
} | ||
return negate ? -x : x; | ||
}, | ||
/** | ||
* Convert to a JS Number. | ||
* | ||
* Be aware that if the returned value is outside the range ... | ||
* | ||
* Int64.MIN_INT <= x <= Int64.MAX_INT | ||
* | ||
* ... it is unlikely to exactly represent the underlying 64-bit value. | ||
* Return string value | ||
*/ | ||
valueOf: function() { | ||
toString: function(radix) { | ||
return this.valueOf().toString(radix || 10); | ||
}, | ||
/** | ||
* Return a string showing the buffer octets, with MSB on the left. | ||
*/ | ||
toOctetString: function(sep) { | ||
var out = new Array(8); | ||
var b = this.buffer, o = this.offset; | ||
var negate = b[0] & 0x80, x = 0, xx = 1; | ||
if (negate) this._2scomp(); | ||
for (var i = o + 7; i >= o; i--) { | ||
var v = b[i] & (i == 0 ? 0x7f : 0xff); | ||
x += v*xx; | ||
xx *= 0x100; | ||
for (var i = 0; i < 8; i++) { | ||
out[i] = _HEX[b[o+i]]; | ||
} | ||
if (negate) { | ||
x = -x; | ||
this._2scomp(); | ||
} | ||
return x; | ||
return out.join(sep || ''); | ||
}, | ||
/** | ||
* Get value as a string of hex octets | ||
* | ||
* @param sep (String) string to join() with. Default='' | ||
* Pretty output in console.log | ||
*/ | ||
toString: function(sep) { | ||
var b = this.buffer, o = this.offset, s = ['0x']; | ||
for (var i = 0; i < 8; i++) { | ||
s.push(_HEX[this.buffer[o+i]]); | ||
} | ||
return s.join(''); | ||
inspect: function() { | ||
return '[Int64 value:' + this + ' octets:' + this.toOctetString(' ') + ']'; | ||
} | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"url" : "http://github.com/broofa/node-int64", | ||
"keywords" : ["math", "integer"], | ||
"keywords" : ["math", "integer", "int64"], | ||
"author" : "Robert Kieffer <robert@broofa.com>", | ||
@@ -12,3 +12,3 @@ "contributors" : [], | ||
"main" : "./Int64.js", | ||
"version" : "0.1.0" | ||
"version" : "0.2.0" | ||
} |
26
test.js
var Int64 = require('./Int64'); | ||
var args = [ | ||
[0], | ||
[1], | ||
[-1], | ||
[1e18], | ||
['0ff1234500654321'], | ||
[0xff12345, 0x654321], | ||
['0x0001234500654321'], | ||
['0xFFFFFFFFFFFFFFFF'] | ||
[0], '0000000000000000', | ||
[1], '0000000000000001', | ||
[-1], 'ffffffffffffffff', | ||
[1e18], '0de0b6b3a7640000', | ||
['0ff1234500654321'], '0ff1234500654321', | ||
[0xff12345, 0x654321], '0ff1234500654321', | ||
['0x0000123450654321'], '0000123450654321', | ||
['0xFFFFFFFFFFFFFFFF'], 'ffffffffffffffff' | ||
]; | ||
for (var i = 0; i < args.length; i++) { | ||
var a = args[i]; | ||
for (var i = 0; i < args.length; i += 2) { | ||
var a = args[i], octets = args[i+1]; | ||
// Create instance | ||
@@ -20,6 +20,8 @@ var x = new Int64(); | ||
console.log(' args: ' + a); | ||
console.log('value: ' + x + ', string: ' + x.toString() + ', err: ' + x.error()); | ||
console.log('new Int64(' + a + ')'); | ||
var pass = x.toOctetString() == octets; | ||
console.log((pass ? 'PASS' : 'FAIL') + ' - value:' + x + | ||
', octets: ' + x.toOctetString()); | ||
console.log('-----------'); | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
8373
4
169
0
70