JSPM

lodash-arithmetic

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 33
  • Score
    100M100P100Q63662F
  • License MIT

Lodash mixins for arbitrary-precision arithmetic

Package Exports

  • lodash-arithmetic

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lodash-arithmetic) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.