better-math
v1.4.3
Published
Math operations more reliable and predictable
Downloads
5
Readme
better-math
Mathematical operations performed reliably and intuitively
Why?
- No ugly approximation:
0.1 + 0.2 // => 0.30000000000000004
math.add(0.1, 0.2) // => 0.3
- No messy unpredictable type conversion:
1 + '1' // => '11'
true - 1 // => 0
[] + 1.1 // => '1.1'
[] - 1.1 // => '-1.1'
{} + 1.1 // => 1.1
math.add(1, '1') // => Uncaught Error: Both arguments must be finite numbers
math.add(true, -1) // => Uncaught Error: Both arguments must be finite numbers
math.add([], 1.1) // => Uncaught Error: Both arguments must be finite numbers
math.add([], 1.1) // => Uncaught Error: Both arguments must be finite numbers
math.add({}, 1.1) // => Uncaught Error: Both arguments must be finite numbers
Install
$ npm install --save better-math
Usage
var math = require('better-math')
math.add(0.1, 0.2) // => 0.3
math.add(0.1, -0.2) // => -0.1
API
math.add (number1, number2)
number1, number2
Type: real number
for both arguments strictly enforced with no exceptions!
No Infinities
, no NaN
s, no null
, no strings (not even '1'
!):
if (!isFinite(x) || !isFinite(y)) throw Error('Both arguments must be finite numbers')
Status
Currently .add
is implemented and thoroughly tested
Credit
- This library is a tiny wrapper around the wonderful arbitrary-precision decimal arithmetic library Big.js that deserves most credit
- The (also wonderful)) LoDash is used for type checking
License
MIT © Dmitri Zaitsev