npm.io
2.3.5 • Published 4 years ago

bcmath

Licence
ISC
Version
2.3.5
Deps
2
Size
56 kB
Vulns
4
Weekly
0

bcmath

total downloads of bcmath bcmath's License latest version of bcmath

Arbitrary-length arithmetics without hassle.

bcmath package is a robust solution for calculations when precision is key.

  • No to floating-point numbers.
  • No to length/precision limits
  • Yes to an API that makes sense.

Examples

import {Bcmath} from 'bcmath'

// Spawn a bcmath instance with a precision of 20 decimal places
const math = Bcmath(20)

console.log(0.1 + 0.2 + 0.3)
// 0.6000000000000001 :-(

console.log(math.chain(0.1).add(0.2).add(0.3).done())
// 0.6 :-)

console.log(math.eval('x ^ (y + 5)', {x: 2, y: 3}))
// 256

console.log(math.pow(2, 4096).length)
// 1234

const n = math.chain(0.15, 50).pow(-10)
console.log(n.done())
// 173415299.15832613592101475046148114277972531287574726074779

console.log(n.round(3).done())
//173415299.158

console.log(n.round(-3).done())
//173415000

console.log(math.max(1, 2, 3.62, 3.61))
// 3.62

console.log(math.pi(50))
// 3.14159265358979323846264338327950288419716939937510

console.log(math.sqrt(2))
// 1.41421568627450980392

API

index.js
Bcmath(scale)

Get a BcmathClass instance

Parameters
Name Type Description
scale int Decimal places  
Returns
  • BcmathClass
new BcmathClass()

Bcmath

Returns
  • Void
BcmathClass.constructor(scale)

Constructor

Parameters
Name Type Description
scale int Decimal places  
Returns
  • Void
BcmathClass.chain(number, scale)

Returns Chain object

Parameters
Name Type Description
number string number BigInt Number to start with  
scale int Number of decimal places  
Returns
  • Chain
BcmathClass.compare(left, right)

Returns: -1 if left is lesser than right 0 if left is equal to right 1 if left is greater than right

Parameters
Name Type Description
left string number BigInt Left operand  
right string number BigInt Right operand  
Returns
  • int
BcmathClass.pow(number, power)

Number to be raised to a power

Parameters
Name Type Description
number string number BigInt Number  
power int Power  
Returns
  • number
BcmathClass.avg(numbers)
Parameters
Name Type Description
numbers  
Returns
  • string
BcmathClass.round(number, precision)

Round the number to the nearest round number

Parameters
Name Type Description
number string number BigInt Number  
precision Number of decimal places. Can be negative. Default: 0  
Returns
  • string
BcmathClass.abs(number)

Returns the absolute value of the specified number

Parameters
Name Type Description
number string number BigInt  
Returns
  • string
BcmathClass.floor(number, precision)

Round the number down

Parameters
Name Type Description
number string number BigInt Subject number  
precision Number of decimal places. Can be negative. Default: 0  
Returns
  • string
BcmathClass.ceil(number, precision)

Round the number up

Parameters
Name Type Description
number string number BigInt Subject number  
precision int Number of decimal places. Can be negative. Default: 0  
Returns
  • string
BcmathClass.mul(number, multiplier, scale)

Multiply

Parameters
Name Type Description
number string number BigInt  
multiplier string number BigInt  
scale int Number of decimal places  
Returns
  • string
BcmathClass.div(number, divisor, scale)

Divide

Parameters
Name Type Description
number string number BigInt Number  
divisor string number BigInt Divisor  
scale int Number of decimal places  
Returns
  • string
BcmathClass.add(left, right, scale)

Add two numbers

Parameters
Name Type Description
left string number BigInt Left operand  
right string number BigInt Right operand  
scale int Number of decimal places  
Returns
  • string
BcmathClass.mod(number, divisor)

Get the modulus

Parameters
Name Type Description
number string number BigInt Number  
divisor Divisor  
Returns
  • string
BcmathClass.sub(left, right, scale)

Substract right from left

Parameters
Name Type Description
left Left operand  
right Right operand  
scale int Number of decimal places  
Returns
  • string
BcmathClass.max(scale)

Returns the highest number

Parameters
Name Type Description
...numbers Array of numbers  
scale int Number of decimal places  
Returns
  • string
BcmathClass.min(scale)

Returns the lowest number

Parameters
Name Type Description
...numbers Array of numbers  
scale int Number of decimal places  
Returns
  • string
BcmathClass.isBigInt(number)

Check if the number fits in a signed BigInt

Parameters
Name Type Description
number string number BigInt Number  
Returns
  • boolean
BcmathClass.isSafeBigInt(number)

Check if the number is safe to use in Javascript BigInt

Parameters
Name Type Description
number string number BigInt Number  
Returns
  • boolean
BcmathClass.*generateDigitsOfPi()
Returns
  • Generator.<number>
BcmathClass.pi(scale)

Get π

Parameters
Name Type Description
scale int Number of decimal places  
Returns
  • string
BcmathClass.piFormatted(scale)

π in a formatted string, up to 50 digits per line

Parameters
Name Type Description
scale int Number of decimal places  
Returns
  • string
BcmathClass.sqrt(number, scale)

Calculate square root

Parameters
Name Type Description
number string number BigInt  
scale int  
Returns
  • string
BcmathClass.neg(number)

Multiply by -1

Parameters
Name Type Description
number string number BigInt Number  
Returns
  • Void
BcmathClass.eval(expr, variables)

Evaluate an expression

Parameters
Name Type Description
expr string Expression, e.g 'x + y'  
variables object  
Returns
  • Void
BcmathClass.parse(expr)
Parameters
Name Type Description
expr  
Returns
  • function(*): *
trimZeroes(value)

Trims empty decimal places

Parameters
Name Type Description
value  
Returns
  • string
chain.js
Chain.constructor(number, scale)

Constructor

Parameters
Name Type Description
number string number BigInt Number  
scale int Number of decimal places (default: 10)  
Returns
  • Void
Chain.toJSON()

toJSON

Returns
  • string
Chain.scale(scale)

Set the scale of operations

Parameters
Name Type Description
scale int Number of decimal places  
Returns
  • Chain
Chain.compare(left, right)

Returns: -1 if current value is lesser than the number 0 if left is equal to the number 1 if left is greater than the number

Parameters
Name Type Description
left Left operand  
right Right operand  
Returns
  • int
Chain.round(precision)

Round value to the nearest round number

Parameters
Name Type Description
precision Number of decimal places. Can be negative. Default: 0  
Returns
  • Chain
Chain.floor(precision)

Round the number down

Parameters
Name Type Description
precision Number of decimal places. Can be negative. Default: 0  
Returns
  • Chain
Chain.ceil(precision)

Round the number up

Parameters
Name Type Description
precision Number of decimal places. Can be negative. Default: 0  
Returns
  • Chain
Chain.pow(power)

Pow

Parameters
Name Type Description
power  
Returns
  • Chain
Chain.mul(value)

Multiply

Parameters
Name Type Description
value  
Returns
  • Chain
Chain.div(divisor)

Divide value by a divisor

Parameters
Name Type Description
divisor Divisor  
Returns
  • Chain
Chain.sub(number)

Substract a number

Parameters
Name Type Description
number string number BigInt Number to add  
Returns
  • Chain
Chain.add(value)

Add a number

Parameters
Name Type Description
value  
Returns
  • Chain
Chain.max(numbers)

Returns the highest number

Parameters
Name Type Description
numbers string number BigInt Array of numbers  
Returns
  • Chain
Chain.min(numbers)

Returns the lowest number

Parameters
Name Type Description
numbers string number BigInt Array of numbers  
Returns
  • Chain
Chain.abs()

Returns the absolute value of the specified number

Returns
  • Chain
Chain.done(plus)

Return the final value of the chain

Parameters
Name Type Description
plus If true, positive number will be prepended by + sign. Default: false  
Returns
  • string
Chain.raw()

Get the raw value

Returns

Documentation generated with doxdox.

Development

GitHub: https://github.com/kakserpom/bcmath.js