Giter VIP home page Giter VIP logo

long.js's Introduction

Long.js

A Long class for representing a 64-bit two's-complement integer value derived from the Closure Library for stand-alone use and extended with unsigned support.

Why?

As of the ECMAScript specification, number types have a maximum value of 2^53. Beyond that, behaviour might be unexpected. Furthermore, bitwise operations are always performed on 32bit numbers. However, in some use cases it is required to be able to perform reliable mathematical and/or bitwise operations on the full 64bits. This is where Long.js comes into play.

Long.js is based on the goog.math.Long class from the Closure Library. It uses two 32bit integers internally and provides methods for comparison, common tests, math and bitwise operations on the full 64bits. Additionally, some use cases also require to work with 64bit unsigned values, so Long.js has been extended with unsigned support while maintaining compatibility to the Closure Library implementation.

Features

  • CommonJS compatible
  • RequireJS/AMD compatible
  • Shim compatible (include the script, then use var Long = dcodeIO.Long;)
  • node.js compatible, also available via npm
  • Fully documented using jsdoc3
  • Zero production dependencies
  • Small footprint

Long

  • Construction from high and low bits as 32bit integers: new Long(low, high[, unsigned=false]) and Long.fromBits(low, high[, unsigned=false])
  • ...from a 32bit integer: Long.fromInt(value[, unsigned=false]) including a cache for frequently used small numbers
  • ...from a number which may internally be a number or double type: Long.fromNumber(value[, unsigned=false])
  • ...from a string: Long.fromString(value[, unsigned=false, radix=10])
  • Conversion to a 32bit integer: Long#toInt()
  • ...to a number: Long#toNumber()
  • ...to a string: Long#toString([radix=10])
  • Getters for high and low bits as 32bit integers: Long#getLowBits(), Long#getHighBits() and Long#getLowBitsUnsigned(), Long#getHighBitsUnsigned()
  • Comparison: Long#equals(other), Long#notEquals(other), Long#lessThan(other), Long#lessThanOrEqual(other), Long#greaterThan(other), Long#greaterThanOrEqual(other), Long#compare(other)
  • Common tests: Long#isZero(), Long#isNegative(), Long#isOdd(), Long#isEven()
  • Math: Long#negate(), Long#add(other), Long#subtract(other), Long#multiply(other), Long#div(other), Long#modulo(other)
  • Bitwise operations: Long#not(), Long#and(other), Long#or(other), Long#xor(other), Long#shiftLeft(numBits), Long#shiftRight(numBits), Long#shiftRightUnsigned(numBits)
  • Conversion between signed and unsinged: Long.toSigned(), Long.toUnsigned()

Usage

node.js / CommonJS

Install: npm install long

var Long = require("long");
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
console.log(longVal.toString());
...

RequireJS / AMD

require.config({
    "paths": {
        "Math/Long": "/path/to/Long.js"
    }
});
require(["Math/Long"], function(Long) {
    var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
    console.log(longVal.toString());
});

Browser / shim

<script src="//raw.github.com/dcodeIO/Long.js/master/Long.min.js"></script>
var Long = dcodeIO.Long;
var longVal = new Long(0xFFFFFFFF, 0x7FFFFFFF);
alert(longVal.toString());

Documentation

Downloads

License

Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html

long.js's People

Contributors

dcodeio avatar adambom avatar

Watchers

James Cloos avatar yunjingyi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.