• This repository has been archived on 18/Dec/2020
  • Stars
    star
    202
  • Rank 193,691 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 14 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Support for representing 64-bit integers in JavaScript

image This package is not actively maintained

int64 was a workaround JS' lack of support for Int64 data types. Somewhat to my surprise, people have found it useful. However, it looks like BigInts will soon be a formally accepted feature, obsoleting the need for this package.

If a trustworthy organization or individual would like to take over as maintainer, I'm happy to hand this off. Please contact me at [email protected] if you're interested. Otherwise I'll be npm-deprecating this at some future date.

int64

JavaScript Numbers are represented as IEEE 754 double-precision floats. 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, a performant, Number-like class is needed. Int64 is that class.

Int64 instances look and feel much like JS-native Numbers. By way of example ...

// First, let's illustrate the problem ...
> (0x123456789).toString(16)
'123456789' // <- what we expect.
> (0x123456789abcdef0).toString(16)
'123456789abcdf00' // <- Ugh!  JS doesn't do big ints. :(

// So let's create a couple Int64s using the above values ...

// Require, of course
> Int64 = require('node-int64')

// x's value is what we expect (the decimal value of 0x123456789)
> x = new Int64(0x123456789)
[Int64 value:4886718345 octets:00 00 00 01 23 45 67 89]

// y's value is Infinity because it's outside the range of integer
// precision.  But that's okay - it's still useful because it's internal
// representation (octets) is what we passed in
> y = new Int64('123456789abcdef0')
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]

// Let's do some math.  Int64's behave like Numbers.  (Sorry, Int64 isn't
// for doing 64-bit integer arithmetic (yet) - it's just for carrying
// around int64 values
> x + 1
4886718346
> y + 1
Infinity

// Int64 string operations ...
> 'value: ' + x
'value: 4886718345'
> 'value: ' + y
'value: Infinity'
> x.toString(2)
'100100011010001010110011110001001'
> y.toString(2)
'Infinity'

// Use JS's isFinite() method to see if the Int64 value is in the
// integer-precise range of JS values
> isFinite(x)
true
> isFinite(y)
false

// Get an octet string representation.  (Yay, y is what we put in!)
> x.toOctetString()
'0000000123456789'
> y.toOctetString()
'123456789abcdef0'

// Finally, some other ways to create Int64s ...

// Pass hi/lo words
> new Int64(0x12345678, 0x9abcdef0)
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]

// Pass a Buffer
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]))
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]

// Pass a Buffer and offset
> new Int64(new Buffer([0,0,0,0,0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0]), 4)
[Int64 value:Infinity octets:12 34 56 78 9a bc de f0]

// Pull out into a buffer
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).toBuffer()
<Buffer 12 34 56 78 9a bc de f0>

// Or copy into an existing one (at an offset)
> var buf = new Buffer(1024);
> new Int64(new Buffer([0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0])).copy(buf, 512);

More Repositories

1

mime

Mime types for JavaScript
TypeScript
2,157
star
2

BroofaJS

Misc. JS utilities that may be of broader interest
JavaScript
212
star
3

jslitmus

Simple, easy, javascript benchmark testing
JavaScript
168
star
4

runmd

Executable markdown files
JavaScript
121
star
5

checkcss

TypeScript
27
star
6

joos

OO in JavaScript
JavaScript
18
star
7

simplur

TypeScript
14
star
8

jarvis

A Search Box Assistant
JavaScript
11
star
9

socipedia

A crowd-sourced business directory web app
PHP
9
star
10

jslife

Conway's Life Simulation, in JavaScript
HTML
6
star
11

proccoli

A customizable web proxy server
JavaScript
5
star
12

airjs

Javascript implementation of the barometric function for determining air density and pressure as a function of altitude
JavaScript
4
star
13

grout

A micro-framework for using CommonJS modules in browsers
3
star
14

mime-score

Logic for prioritizing MIME types based on facet, source, and type
TypeScript
3
star
15

jsscore

A debatable indicator of Javascript performance
JavaScript
3
star
16

jsciiart

Generate ascii art in realtime from webcamera in the browser using JavaScript + WebRTC (getUserMedia)
JavaScript
3
star
17

lzwjs

lzwjs
JavaScript
3
star
18

resnap

Simple API for capturing and resetting Node's require() cache state
JavaScript
3
star
19

plague

JavaScript
2
star
20

streamie

App for testing streaming transports in IE
JavaScript
2
star
21

flightcard

TypeScript
2
star
22

pek

A modern data model for UI rendering
JavaScript
2
star
23

storagehooks

JavaScript
1
star
24

perf-lite

Lightweight language performance test
C++
1
star
25

gmaul

TypeScript
1
star
26

stringlang

Utility functions for analyzing strings by Unicode block
JavaScript
1
star
27

oshareport

The Oregon OSHA report form, reskinned for COVID enforcement
HTML
1
star
28

thrustcurve-db

JavaScript
1
star