1.1.1 • Published 5 years ago
decimath v1.1.1
DeciMath
A small JavaScript library for doing precise floating point maths
Sure, there are many maths libraries to tickle your fancy, but sometimes you just need proper floating point maths and nothing more, that's why this library exists.
Install
Node/React
npm install decimathyarn add decimathBrowser
Just copy and link to the file or include it in your scripts.
Usage
Node Only:
import BigNumber from 'decimath';You'll need a BigNumber to make use of this. To make a new Big Number Use the following:
let bn = BigNumber(0.1); // initial valueOperations
All operations support a value as either a BigNumber or any other numeric type that can be converted to a BigNumber.
All operations except Power Of accept decimal values.
All operations return this to allow chaining. To Get the value, end the chain with .valueOf()
Add
bn.add(0.2); // adds 0.2 to our initial valueSubtract
bn.subtract(0.1); // Takes 0.1 from our valueMultiply
bn.multiply(100); // Multiplies value by 100Divide
bn.divide(2.5); // Divides our value by 2.5Power of
bn.pow(30); // bn to the power of 30Using calculated values outside of the library
Use .valueOf() to get the JS decimal version of the number
