Code Monkey home page Code Monkey logo

lodash-arithmetic's Introduction

lodash-arithmetic

Build Status Coveralls npm version npm downloads npm dependencies npm devDependencies npm license

Lodash mixins for arbitrary-precision arithmetic

Why Lo-Dash mixins for arithmetic?

Because of binary floating-point, arithmetic operations in JavaScript may imply rounding issues like:

0.1 + 0.2 = 0.30000000000000004

You will find a complete explanation about floating-point arithmetic at http://floating-point-gui.de/.

Lodash-arithmetic provides simple Lo-Dash mixins for basic arithmetic operations: addition, subtraction, multiplication and division. Even if Lo-Dash 4 natively has the methods _.add, _.subtract, _.multiply and _.divide, they do not take in account arbitrary-precision decimal.

Let's see the following examples:

// Without Lo-Dash
console.log(0.1 + 0.2 === 0.3) // false

// With Lo-Dash 4 but without the arithmetic mixins
console.log(_.add(0.1, 0.2) === 0.3) // false

// With Lo-Dash and the arithmetic mixins
console.log(_.add(0.1, 0.2) === 0.3) // true

Install

The easiest way is to install lodash-arithmetic as dependency:

npm install lodash-arithmetic --save

Usage

Addition
_.add(0.1, 0.2); // 0.3
Subtraction
_.subtract(0.1235, 0.1234); // 0.0001
Multiplication
_.multiply(0.07, 100); // 7
Division
_.divide(8.2, 1000); // 0.0082
Chaining
_(0.1)
  .add(0.2)
  .multiply(77.1)
  .subtract(0.12345)
  .value(); // 23.00655

License

Code licensed under MIT License.

lodash-arithmetic's People

Contributors

yannickglt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

lodash-arithmetic's Issues

multiply does not return fraction, it rounds, needs option to return fraction.

Great job, one problem with JavaScript and Floating point:

Example: multiply("241828072282107.5071453596951","666")
expected value: 161057496139883599.758809556936599999999598382266485785407894582022
returned value: 161057496139883600

(ia * ib) / (fa * fb)
(2418280722821075071453596951 * 666) / (10000000000000 / 1)
= 161057496139883599.758809556936599999999598382266485785407894582022
This is what I want it to return,
It is not the math that is failing,
it is JavaScript, it can not handle this large of Float, so it rounds it
toFixed has no effect.

Any idea how to get JavaScript to do this math without Rounding it?

truncate

Add a truncate function to cut the number?

Example:

_(0.112).add(1.11).truncate(2) ---> 1.22

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.