utils for safe arithmetics around floating point numbers
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
If any issue or bugs found while using SafeMaths do open an issue and report it there.
$ yarn add safe-mts
$ npm install safe-mts
Takes two numbers and 'safely' adds them.
Example
import { safeAdd } from "safe-mts";
console.log(0.1 + 0.7) // 0.7999999999999999
console.log(safeAdd(0.1,0.7)) // 0.8
Example
import { safeSubtract } from "safe-mts";
console.log(0.7 - 0.2) // 0.49999999999999994
console.log(safeAdd(0.7,0.2)) // 0.5
Takes two numbers and 'safely' multiplies them.
Example
import { safeMultiply } from "safe-mts";
console.log(0.1 * 0.7) // 0.06999999999999999
console.log(safeMultiply(0.1,0.7)) // 0.07
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)) // 1
console.log(decimalCount(0.01)) // 2
console.log(decimalCount(0.001)) // 3
console.log(decimalCount(0.0001)) // 4