Skip to content

Commit ec813a5

Browse files
committed
Notes on NaN
1 parent 7a3148e commit ec813a5

File tree

6 files changed

+55
-24
lines changed

6 files changed

+55
-24
lines changed

Long.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353
* case would often result in infinite recursion.
5454
*
5555
* @exports Long
56+
* @class A Long class for representing a 64-bit two's-complement integer value.
5657
* @param {number} low The low (signed) 32 bits of the long.
5758
* @param {number} high The high (signed) 32 bits of the long.
58-
* @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed).
59+
* @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed).
5960
* @constructor
6061
*/
6162
var Long = function(low, high, unsigned) {
@@ -107,24 +108,25 @@
107108
* @expose
108109
*/
109110
Long.fromInt = function(value, unsigned) {
111+
var obj, cachedObj;
110112
if (!unsigned) {
111113
value = value | 0;
112114
if (-128 <= value && value < 128) {
113-
var cachedObj = INT_CACHE[value];
115+
cachedObj = INT_CACHE[value];
114116
if (cachedObj) return cachedObj;
115117
}
116-
var obj = new Long(value, value < 0 ? -1 : 0, false);
118+
obj = new Long(value, value < 0 ? -1 : 0, false);
117119
if (-128 <= value && value < 128) {
118120
INT_CACHE[value] = obj;
119121
}
120122
return obj;
121123
} else {
122124
value = value >>> 0;
123125
if (0 <= value && value < 256) {
124-
var cachedObj = UINT_CACHE[value];
126+
cachedObj = UINT_CACHE[value];
125127
if (cachedObj) return cachedObj;
126128
}
127-
var obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
129+
obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
128130
if (0 <= value && value < 256) {
129131
UINT_CACHE[value] = obj;
130132
}
@@ -201,7 +203,10 @@
201203
if (str.length == 0) {
202204
throw(new Error('number format error: empty string'));
203205
}
204-
if (typeof unsigned == 'number') { // For goog.math.Long compatibility
206+
if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") {
207+
return Long.ZERO;
208+
}
209+
if (typeof unsigned === 'number') { // For goog.math.Long compatibility
205210
radix = unsigned;
206211
unsigned = false;
207212
}
@@ -370,13 +375,14 @@
370375
if (this.isZero()) {
371376
return '0';
372377
}
378+
var rem;
373379
if (this.isNegative()) { // Unsigned Longs are never negative
374380
if (this.equals(Long.MIN_SIGNED_VALUE)) {
375381
// We need to change the Long value before it can be negated, so we remove
376382
// the bottom-most digit in this base and then recurse to do the rest.
377383
var radixLong = Long.fromNumber(radix);
378384
var div = this.div(radixLong);
379-
var rem = div.multiply(radixLong).subtract(this);
385+
rem = div.multiply(radixLong).subtract(this);
380386
return div.toString(radix) + rem.toInt().toString(radix);
381387
} else {
382388
return '-' + this.negate().toString(radix);
@@ -386,7 +392,7 @@
386392
// Do several (6) digits each time through the loop, so as to
387393
// minimize the calls to the very expensive emulated div.
388394
var radixToPower = Long.fromNumber(Math.pow(radix, 6));
389-
var rem = this;
395+
rem = this;
390396
var result = '';
391397
while (true) {
392398
var remDiv = rem.div(radixToPower);

Long.min.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Features
1717
* Shim compatible (include the script, then use var Long = dcodeIO.Long;)
1818
* [node.js](http://nodejs.org) compatible, also available via [npm](https://npmjs.org/package/long)
1919
* Fully documented using [jsdoc3](https://github.com/jsdoc3/jsdoc)
20+
* API-compatible to the Closure Library implementation
2021
* Zero production dependencies
2122
* Small footprint
2223

docs/Long.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ <h2>
3030
Long
3131
</h2>
3232

33+
<div class="class-description">A Long class for representing a 64-bit two's-complement integer value.</div>
34+
3335
</header>
3436

3537
<article>
@@ -181,7 +183,7 @@ <h5>Parameters:</h5>
181183

182184

183185

184-
<td class="description last">Whether unsigned or not. Defaults to false (signed).</td>
186+
<td class="description last">Whether unsigned or not. Defaults to `false` (signed).</td>
185187
</tr>
186188

187189

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "long",
33
"version": "1.1.2",
44
"author": "Daniel Wirtz <[email protected]>",
5-
"description": "Long.js: A Long class for representing a 64-bit two's-complement integer value derived from the Closure Library extended with unsigned support.",
5+
"description": "A Long class for representing a 64-bit two's-complement integer value.",
66
"main": "Long.js",
77
"repository": {
88
"type": "git",

0 commit comments

Comments
 (0)