2.3.5 • Published 2 years ago

bcmath v2.3.5

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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
NameTypeDescription
scaleintDecimal places 
Returns
  • BcmathClass

new BcmathClass()

Bcmath

Returns
  • Void

BcmathClass.constructor(scale)

Constructor

Parameters
NameTypeDescription
scaleintDecimal places 
Returns
  • Void

BcmathClass.chain(number, scale)

Returns Chain object

Parameters
NameTypeDescription
numberstring number BigIntNumber to start with 
scaleintNumber 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
NameTypeDescription
leftstring number BigIntLeft operand 
rightstring number BigIntRight operand 
Returns
  • int

BcmathClass.pow(number, power)

Number to be raised to a power

Parameters
NameTypeDescription
numberstring number BigIntNumber 
powerintPower 
Returns
  • number

BcmathClass.avg(numbers)

Parameters
NameTypeDescription
numbers 
Returns
  • string

BcmathClass.round(number, precision)

Round the number to the nearest round number

Parameters
NameTypeDescription
numberstring number BigIntNumber 
precisionNumber of decimal places. Can be negative. Default: 0 
Returns
  • string

BcmathClass.abs(number)

Returns the absolute value of the specified number

Parameters
NameTypeDescription
numberstring number BigInt 
Returns
  • string

BcmathClass.floor(number, precision)

Round the number down

Parameters
NameTypeDescription
numberstring number BigIntSubject number 
precisionNumber of decimal places. Can be negative. Default: 0 
Returns
  • string

BcmathClass.ceil(number, precision)

Round the number up

Parameters
NameTypeDescription
numberstring number BigIntSubject number 
precisionintNumber of decimal places. Can be negative. Default: 0 
Returns
  • string

BcmathClass.mul(number, multiplier, scale)

Multiply

Parameters
NameTypeDescription
numberstring number BigInt 
multiplierstring number BigInt 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.div(number, divisor, scale)

Divide

Parameters
NameTypeDescription
numberstring number BigIntNumber 
divisorstring number BigIntDivisor 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.add(left, right, scale)

Add two numbers

Parameters
NameTypeDescription
leftstring number BigIntLeft operand 
rightstring number BigIntRight operand 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.mod(number, divisor)

Get the modulus

Parameters
NameTypeDescription
numberstring number BigIntNumber 
divisorDivisor 
Returns
  • string

BcmathClass.sub(left, right, scale)

Substract right from left

Parameters
NameTypeDescription
leftLeft operand 
rightRight operand 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.max(scale)

Returns the highest number

Parameters
NameTypeDescription
...numbersArray of numbers 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.min(scale)

Returns the lowest number

Parameters
NameTypeDescription
...numbersArray of numbers 
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.isBigInt(number)

Check if the number fits in a signed BigInt

Parameters
NameTypeDescription
numberstring number BigIntNumber 
Returns
  • boolean

BcmathClass.isSafeBigInt(number)

Check if the number is safe to use in Javascript BigInt

Parameters
NameTypeDescription
numberstring number BigIntNumber 
Returns
  • boolean

BcmathClass.*generateDigitsOfPi()

Returns
  • Generator.<number>

BcmathClass.pi(scale)

Get π

Parameters
NameTypeDescription
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.piFormatted(scale)

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

Parameters
NameTypeDescription
scaleintNumber of decimal places 
Returns
  • string

BcmathClass.sqrt(number, scale)

Calculate square root

Parameters
NameTypeDescription
numberstring number BigInt 
scaleint 
Returns
  • string

BcmathClass.neg(number)

Multiply by -1

Parameters
NameTypeDescription
numberstring number BigIntNumber 
Returns
  • Void

BcmathClass.eval(expr, variables)

Evaluate an expression

Parameters
NameTypeDescription
exprstringExpression, e.g 'x + y' 
variablesobject 
Returns
  • Void

BcmathClass.parse(expr)

Parameters
NameTypeDescription
expr 
Returns
  • function(*): *

trimZeroes(value)

Trims empty decimal places

Parameters
NameTypeDescription
value 
Returns
  • string

chain.js

Chain.constructor(number, scale)

Constructor

Parameters
NameTypeDescription
numberstring number BigIntNumber 
scaleintNumber of decimal places (default: 10) 
Returns
  • Void

Chain.toJSON()

toJSON

Returns
  • string

Chain.scale(scale)

Set the scale of operations

Parameters
NameTypeDescription
scaleintNumber 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
NameTypeDescription
leftLeft operand 
rightRight operand 
Returns
  • int

Chain.round(precision)

Round value to the nearest round number

Parameters
NameTypeDescription
precisionNumber of decimal places. Can be negative. Default: 0 
Returns
  • Chain

Chain.floor(precision)

Round the number down

Parameters
NameTypeDescription
precisionNumber of decimal places. Can be negative. Default: 0 
Returns
  • Chain

Chain.ceil(precision)

Round the number up

Parameters
NameTypeDescription
precisionNumber of decimal places. Can be negative. Default: 0 
Returns
  • Chain

Chain.pow(power)

Pow

Parameters
NameTypeDescription
power 
Returns
  • Chain

Chain.mul(value)

Multiply

Parameters
NameTypeDescription
value 
Returns
  • Chain

Chain.div(divisor)

Divide value by a divisor

Parameters
NameTypeDescription
divisorDivisor 
Returns
  • Chain

Chain.sub(number)

Substract a number

Parameters
NameTypeDescription
numberstring number BigIntNumber to add 
Returns
  • Chain

Chain.add(value)

Add a number

Parameters
NameTypeDescription
value 
Returns
  • Chain

Chain.max(numbers)

Returns the highest number

Parameters
NameTypeDescription
numbersstring number BigIntArray of numbers 
Returns
  • Chain

Chain.min(numbers)

Returns the lowest number

Parameters
NameTypeDescription
numbersstring number BigIntArray 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
NameTypeDescription
plusIf 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

2.3.5

2 years ago

2.3.4

2 years ago

2.3.3

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.0

2 years ago

2.2.1

2 years ago

2.1.1

2 years ago

2.1.0

2 years ago

2.0.9

2 years ago

2.0.9-n

2 years ago

2.0.8

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago