Code Monkey home page Code Monkey logo

jslongarithmetic's Introduction

JS Long Arithmetic

This lib provides arithmetical operation with numbers of different length and precision. It was written on Ecmascript 6. You can watch tests and try it here (your browser has to support ES6.

Usage

Just add longArithmetic.js to your project.

ES6 in browsers

Chrome enable chrome://flags/#enable-javascript-harmony

Firefox avaliable only in Firefox Developer Edition

Constructor

new LongNumber(number) creates instance of a class. With no arguments creates number 0. Argument can be both String or Number. You can add exponent too new LongNumber('-4.35e-30').

Static methods

compare(a, b) compares two long numbers. Returns 1 ==> a > b, 0 ==> a == b, -1 ==> a < b.

absCompare(a, b) works as compare(). Comparing by absolute value.

toLongNumber(n) Type conversion.

Methods below return new instance of LongNumber after doind operation

add(a, b)

subtract(a, b)

multiply(a, b)

divide(a, b, precision) (by default precision is 100)

Methods

toString returns LongNumber as a string.

invert() returns this number multiplyed by -1.

clone() returns copy of current item.

Methods below work as LongNumber.method(this, n)

add(n)

subtract(n)

multiply(n)

divide(n, precision)

And some boolean methods:

isZero() isOne() isMinusOne() isPositive() isNegative()

Tips

All methods can get both Number and LongNumber as arguments:

a.add(new LongNumber(32)) is equal to a.add(32)

You can create chains with methods:

a.add(123).multiply(b).invert() for -((a + 123) * b)

jslongarithmetic's People

Contributors

felytic avatar

Stargazers

VINO avatar

Watchers

 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.