Skip to content

Commit

Permalink
Notes on NaN
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 9, 2013
1 parent 7a3148e commit ec813a5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 24 deletions.
22 changes: 14 additions & 8 deletions Long.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@
* case would often result in infinite recursion.
*
* @exports Long
* @class A Long class for representing a 64-bit two's-complement integer value.
* @param {number} low The low (signed) 32 bits of the long.
* @param {number} high The high (signed) 32 bits of the long.
* @param {boolean=} unsigned Whether unsigned or not. Defaults to false (signed).
* @param {boolean=} unsigned Whether unsigned or not. Defaults to `false` (signed).
* @constructor
*/
var Long = function(low, high, unsigned) {
Expand Down Expand Up @@ -107,24 +108,25 @@
* @expose
*/
Long.fromInt = function(value, unsigned) {
var obj, cachedObj;
if (!unsigned) {
value = value | 0;
if (-128 <= value && value < 128) {
var cachedObj = INT_CACHE[value];
cachedObj = INT_CACHE[value];
if (cachedObj) return cachedObj;
}
var obj = new Long(value, value < 0 ? -1 : 0, false);
obj = new Long(value, value < 0 ? -1 : 0, false);
if (-128 <= value && value < 128) {
INT_CACHE[value] = obj;
}
return obj;
} else {
value = value >>> 0;
if (0 <= value && value < 256) {
var cachedObj = UINT_CACHE[value];
cachedObj = UINT_CACHE[value];
if (cachedObj) return cachedObj;
}
var obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
obj = new Long(value, (value | 0) < 0 ? -1 : 0, true);
if (0 <= value && value < 256) {
UINT_CACHE[value] = obj;
}
Expand Down Expand Up @@ -201,7 +203,10 @@
if (str.length == 0) {
throw(new Error('number format error: empty string'));
}
if (typeof unsigned == 'number') { // For goog.math.Long compatibility
if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity") {
return Long.ZERO;
}
if (typeof unsigned === 'number') { // For goog.math.Long compatibility
radix = unsigned;
unsigned = false;
}
Expand Down Expand Up @@ -370,13 +375,14 @@
if (this.isZero()) {
return '0';
}
var rem;
if (this.isNegative()) { // Unsigned Longs are never negative
if (this.equals(Long.MIN_SIGNED_VALUE)) {
// We need to change the Long value before it can be negated, so we remove
// the bottom-most digit in this base and then recurse to do the rest.
var radixLong = Long.fromNumber(radix);
var div = this.div(radixLong);
var rem = div.multiply(radixLong).subtract(this);
rem = div.multiply(radixLong).subtract(this);
return div.toString(radix) + rem.toInt().toString(radix);
} else {
return '-' + this.negate().toString(radix);
Expand All @@ -386,7 +392,7 @@
// Do several (6) digits each time through the loop, so as to
// minimize the calls to the very expensive emulated div.
var radixToPower = Long.fromNumber(Math.pow(radix, 6));
var rem = this;
rem = this;
var result = '';
while (true) {
var remDiv = rem.div(radixToPower);
Expand Down
28 changes: 14 additions & 14 deletions Long.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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

Expand Down
4 changes: 3 additions & 1 deletion docs/Long.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ <h2>
Long
</h2>

<div class="class-description">A Long class for representing a 64-bit two's-complement integer value.</div>

</header>

<article>
Expand Down Expand Up @@ -181,7 +183,7 @@ <h5>Parameters:</h5>



<td class="description last">Whether unsigned or not. Defaults to false (signed).</td>
<td class="description last">Whether unsigned or not. Defaults to `false` (signed).</td>
</tr>


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "long",
"version": "1.1.2",
"author": "Daniel Wirtz <dcode@dcode.io>",
"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.",
"description": "A Long class for representing a 64-bit two's-complement integer value.",
"main": "Long.js",
"repository": {
"type": "git",
Expand Down

0 comments on commit ec813a5

Please sign in to comment.