0.4.0 • Published 8 years ago

node-int64 v0.4.0

Weekly downloads
10,253,567
License
MIT
Repository
github
Last release
8 years ago

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);
bserreact-native-bluetooth2killi8n-react-native-fast-imagejs-v2rayrn-send-smscompaneo-jshs2specify-importsbabel-specify-imports@icanpm/api-masterthrift-trunkreact-native-template-rfbaseairscanairscan-examplereact-native-esc-pos-sahaab@borisovart/atol-kkt-moduledeneme323112@ntt_app/react-native-custom-notificationreact-native-custom-text-hwjamesreact-native-covid-sdkgql_din_modbitgetreact-native-thanh-toast-library@thanhnguyen14797/react-native-thanh-toast-libraryaddr2lineallex_bufferliballex_leveldblib@xswap/token-listsagentvsagent@olivervorasai/sliderreact-native-printer-brothersrn-pdf-reader-offlineant-design-draggable-modal-4ant-design-draggable-modal-fixant-design-draggable-modal-fix-2amqp10amqp-nodejsreact-native-shekhar-bridge-test@felipesimmi/react-native-datalogic-module@fedorg/openapi-typescript-fetch@fangdh/typescript-esbuild-2021candlelabssdk@enmeshed/jaeger-client@enkeledi/react-native-week-month-date-picker@eliteswap/token-lists@dvirtz/parquets@vicapow-org/browser-thrift@vicapow-org/thriftdrpcjs@vitali_shcherbina/styled-lib@vality/woodybrowser-thriftburningpigcordova-plugin-amplify-paycordova-plugin-amplify-paymentconway-gamecaymland-trackingcorfudb-node-sequencercotrader-dex-sdkcouchtapcpcs6_loggerch3ckcfb-cms-democs188-spring2022aws-cdk-organization-configruleaxonv2sdkbabyloniababel-plugin-specify-importsbirken-react-native-community-image-editorbloatedmathasync-busastra-ufo-sdk@smartpartner/postdirekt-autocomplete@sothebys/jaeger-clientdate-library-aadate_operations_manipulatordataormdiscordjs-con-selfdistributed-trace-for-nodejsdiscord.js-byconeson-binaryevanutilsex-ikon-components-libraryfastmqcompress-liuepm-npm-tscfn-lib-examplefront-end-engineeringllrp-tsmd-links-cmd-vditormbed-edge-jsmegazord-javascriptmapleendianmanta-thothmap-values-resolverlinker-sdkmediawarpgoogle-remakegcloudgalaxy-sdk-nodejs
0.4.0

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

9 years ago

0.3.0

11 years ago

0.2.0

12 years ago

0.1.0

12 years ago