Projekt

Obecné

Profil

Stáhnout (9.43 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
long.js
2
=======
3
4
A Long class for representing a 64 bit two's-complement integer value derived from the [Closure Library](https://github.com/google/closure-library)
5
for stand-alone use and extended with unsigned support.
6
7
[![npm](https://img.shields.io/npm/v/long.svg)](https://www.npmjs.com/package/long) [![Build Status](https://travis-ci.org/dcodeIO/long.js.svg)](https://travis-ci.org/dcodeIO/long.js)
8
9
Background
10
----------
11
12
As of [ECMA-262 5th Edition](http://ecma262-5.com/ELS5_HTML.htm#Section_8.5), "all the positive and negative integers
13
whose magnitude is no greater than 2<sup>53</sup> are representable in the Number type", which is "representing the
14
doubleprecision 64-bit format IEEE 754 values as specified in the IEEE Standard for Binary Floating-Point Arithmetic".
15
The [maximum safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)
16
in JavaScript is 2<sup>53</sup>-1.
17
18
Example: 2<sup>64</sup>-1 is 1844674407370955**1615** but in JavaScript it evaluates to 1844674407370955**2000**.
19
20
Furthermore, bitwise operators in JavaScript "deal only with integers in the range −2<sup>31</sup> through
21
2<sup>31</sup>−1, inclusive, or in the range 0 through 2<sup>32</sup>−1, inclusive. These operators accept any value of
22
the Number type but first convert each such value to one of 2<sup>32</sup> integer values."
23
24
In some use cases, however, it is required to be able to reliably work with and perform bitwise operations on the full
25
64 bits. This is where long.js comes into play.
26
27
Usage
28
-----
29
30
The class is compatible with CommonJS and AMD loaders and is exposed globally as `Long` if neither is available.
31
32
```javascript
33
var Long = require("long");
34
35
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
36
37
console.log(longVal.toString());
38
...
39
```
40
41
API
42
---
43
44
### Constructor
45
46
* new **Long**(low: `number`, high: `number`, unsigned?: `boolean`)<br />
47
  Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers. See the from* functions below for more convenient ways of constructing Longs.
48
49
### Fields
50
51
* Long#**low**: `number`<br />
52
  The low 32 bits as a signed value.
53
54
* Long#**high**: `number`<br />
55
  The high 32 bits as a signed value.
56
57
* Long#**unsigned**: `boolean`<br />
58
  Whether unsigned or not.
59
60
### Constants
61
62
* Long.**ZERO**: `Long`<br />
63
  Signed zero.
64
65
* Long.**ONE**: `Long`<br />
66
  Signed one.
67
68
* Long.**NEG_ONE**: `Long`<br />
69
  Signed negative one.
70
71
* Long.**UZERO**: `Long`<br />
72
  Unsigned zero.
73
74
* Long.**UONE**: `Long`<br />
75
  Unsigned one.
76
77
* Long.**MAX_VALUE**: `Long`<br />
78
  Maximum signed value.
79
80
* Long.**MIN_VALUE**: `Long`<br />
81
  Minimum signed value.
82
83
* Long.**MAX_UNSIGNED_VALUE**: `Long`<br />
84
  Maximum unsigned value.
85
86
### Utility
87
88
* Long.**isLong**(obj: `*`): `boolean`<br />
89
  Tests if the specified object is a Long.
90
91
* Long.**fromBits**(lowBits: `number`, highBits: `number`, unsigned?: `boolean`): `Long`<br />
92
  Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is assumed to use 32 bits.
93
94
* Long.**fromBytes**(bytes: `number[]`, unsigned?: `boolean`, le?: `boolean`): `Long`<br />
95
  Creates a Long from its byte representation.
96
97
* Long.**fromBytesLE**(bytes: `number[]`, unsigned?: `boolean`): `Long`<br />
98
  Creates a Long from its little endian byte representation.
99
100
* Long.**fromBytesBE**(bytes: `number[]`, unsigned?: `boolean`): `Long`<br />
101
  Creates a Long from its big endian byte representation.
102
103
* Long.**fromInt**(value: `number`, unsigned?: `boolean`): `Long`<br />
104
  Returns a Long representing the given 32 bit integer value.
105
106
* Long.**fromNumber**(value: `number`, unsigned?: `boolean`): `Long`<br />
107
  Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
108
109
* Long.**fromString**(str: `string`, unsigned?: `boolean`, radix?: `number`)<br />
110
  Long.**fromString**(str: `string`, radix: `number`)<br />
111
  Returns a Long representation of the given string, written using the specified radix.
112
113
* Long.**fromValue**(val: `*`, unsigned?: `boolean`): `Long`<br />
114
  Converts the specified value to a Long using the appropriate from* function for its type.
115
116
### Methods
117
118
* Long#**add**(addend: `Long | number | string`): `Long`<br />
119
  Returns the sum of this and the specified Long.
120
121
* Long#**and**(other: `Long | number | string`): `Long`<br />
122
  Returns the bitwise AND of this Long and the specified.
123
124
* Long#**compare**/**comp**(other: `Long | number | string`): `number`<br />
125
  Compares this Long's value with the specified's. Returns `0` if they are the same, `1` if the this is greater and `-1` if the given one is greater.
126
127
* Long#**divide**/**div**(divisor: `Long | number | string`): `Long`<br />
128
  Returns this Long divided by the specified.
129
130
* Long#**equals**/**eq**(other: `Long | number | string`): `boolean`<br />
131
  Tests if this Long's value equals the specified's.
132
133
* Long#**getHighBits**(): `number`<br />
134
  Gets the high 32 bits as a signed integer.
135
136
* Long#**getHighBitsUnsigned**(): `number`<br />
137
  Gets the high 32 bits as an unsigned integer.
138
139
* Long#**getLowBits**(): `number`<br />
140
  Gets the low 32 bits as a signed integer.
141
142
* Long#**getLowBitsUnsigned**(): `number`<br />
143
  Gets the low 32 bits as an unsigned integer.
144
145
* Long#**getNumBitsAbs**(): `number`<br />
146
  Gets the number of bits needed to represent the absolute value of this Long.
147
148
* Long#**greaterThan**/**gt**(other: `Long | number | string`): `boolean`<br />
149
  Tests if this Long's value is greater than the specified's.
150
151
* Long#**greaterThanOrEqual**/**gte**/**ge**(other: `Long | number | string`): `boolean`<br />
152
  Tests if this Long's value is greater than or equal the specified's.
153
154
* Long#**isEven**(): `boolean`<br />
155
  Tests if this Long's value is even.
156
157
* Long#**isNegative**(): `boolean`<br />
158
  Tests if this Long's value is negative.
159
160
* Long#**isOdd**(): `boolean`<br />
161
  Tests if this Long's value is odd.
162
163
* Long#**isPositive**(): `boolean`<br />
164
  Tests if this Long's value is positive.
165
166
* Long#**isZero**/**eqz**(): `boolean`<br />
167
  Tests if this Long's value equals zero.
168
169
* Long#**lessThan**/**lt**(other: `Long | number | string`): `boolean`<br />
170
  Tests if this Long's value is less than the specified's.
171
172
* Long#**lessThanOrEqual**/**lte**/**le**(other: `Long | number | string`): `boolean`<br />
173
  Tests if this Long's value is less than or equal the specified's.
174
175
* Long#**modulo**/**mod**/**rem**(divisor: `Long | number | string`): `Long`<br />
176
  Returns this Long modulo the specified.
177
178
* Long#**multiply**/**mul**(multiplier: `Long | number | string`): `Long`<br />
179
  Returns the product of this and the specified Long.
180
181
* Long#**negate**/**neg**(): `Long`<br />
182
  Negates this Long's value.
183
184
* Long#**not**(): `Long`<br />
185
  Returns the bitwise NOT of this Long.
186
187
* Long#**notEquals**/**neq**/**ne**(other: `Long | number | string`): `boolean`<br />
188
  Tests if this Long's value differs from the specified's.
189
190
* Long#**or**(other: `Long | number | string`): `Long`<br />
191
  Returns the bitwise OR of this Long and the specified.
192
193
* Long#**shiftLeft**/**shl**(numBits: `Long | number | string`): `Long`<br />
194
  Returns this Long with bits shifted to the left by the given amount.
195
196
* Long#**shiftRight**/**shr**(numBits: `Long | number | string`): `Long`<br />
197
  Returns this Long with bits arithmetically shifted to the right by the given amount.
198
199
* Long#**shiftRightUnsigned**/**shru**/**shr_u**(numBits: `Long | number | string`): `Long`<br />
200
  Returns this Long with bits logically shifted to the right by the given amount.
201
202
* Long#**rotateLeft**/**rotl**(numBits: `Long | number | string`): `Long`<br />
203
  Returns this Long with bits rotated to the left by the given amount.
204
205
* Long#**rotateRight**/**rotr**(numBits: `Long | number | string`): `Long`<br />
206
  Returns this Long with bits rotated to the right by the given amount.
207
208
* Long#**subtract**/**sub**(subtrahend: `Long | number | string`): `Long`<br />
209
  Returns the difference of this and the specified Long.
210
211
* Long#**toBytes**(le?: `boolean`): `number[]`<br />
212
  Converts this Long to its byte representation.
213
214
* Long#**toBytesLE**(): `number[]`<br />
215
  Converts this Long to its little endian byte representation.
216
217
* Long#**toBytesBE**(): `number[]`<br />
218
  Converts this Long to its big endian byte representation.
219
220
* Long#**toInt**(): `number`<br />
221
  Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
222
223
* Long#**toNumber**(): `number`<br />
224
  Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
225
226
* Long#**toSigned**(): `Long`<br />
227
  Converts this Long to signed.
228
229
* Long#**toString**(radix?: `number`): `string`<br />
230
  Converts the Long to a string written in the specified radix.
231
232
* Long#**toUnsigned**(): `Long`<br />
233
  Converts this Long to unsigned.
234
235
* Long#**xor**(other: `Long | number | string`): `Long`<br />
236
  Returns the bitwise XOR of this Long and the given one.
237
238
WebAssembly support
239
-------------------
240
241
[WebAssembly](http://webassembly.org) supports 64-bit integer arithmetic out of the box, hence a [tiny WebAssembly module](./src/wasm.wat) is used to compute operations like multiplication, division and remainder more efficiently (slow operations like division are around twice as fast), falling back to floating point based computations in JavaScript where WebAssembly is not yet supported, e.g., in older versions of node.
242
243
Building
244
--------
245
246
To build an UMD bundle to `dist/long.js`, run:
247
248
```
249
$> npm install
250
$> npm run build
251
```
252
253
Running the [tests](./tests):
254
255
```
256
$> npm test
257
```