Safe Maths
utils for safe arithmetics around floating point numbers
Content
Getting started
SafeMaths provides utility function to carry out arithmetic operations such as multiplication, addition and subtraction
// addition
console.log(0.1 + 0.2) // 0.30000000000000004
console.log(safeAdd(0.1,0.2)) // 0.3 ✅
// subtraction
console.log(0.3 - 0.1) // 0.19999999999999998
console.log(safeSubtract(0.3,0.1)) // 0.2 ✅
// multiplication
console.log(0.1 * 0.4) // 0.04000000000000001
console.log(safeSubtract(0.1,0.4)) // 0.04 ✅
Issues
If any issue or bugs found while using SafeMaths do open an issue and report it there.
Installation
Using yarn
$ yarn add safe-mts
Using npm
$ npm install safe-mts
Functions
safeAdd
Takes two numbers and 'safely' adds them.
Example
import { safeAdd } from "safe-mts";
console.log(0.1 + 0.7)
console.log(safeAdd(0.1,0.7))
safeSubtract
Example
import { safeSubtract } from "safe-mts";
console.log(0.7 - 0.2)
console.log(safeAdd(0.7,0.2))
safeMultiply
Takes two numbers and 'safely' multiplies them.
Example
import { safeMultiply } from "safe-mts";
console.log(0.1 * 0.7)
console.log(safeMultiply(0.1,0.7))
decimalCount
Returns the number of decimal places of a number. i.e 1 if 1dp, 2 if 2dp and so on.
Example
import { decimalCount } from "safe-mts";
console.log(decimalCount(0.1))
console.log(decimalCount(0.01))
console.log(decimalCount(0.001))
console.log(decimalCount(0.0001))