npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bcmath

v2.3.5

Published

Arbitrary-length arithmetics API based on locutus/php/bcmath

Downloads

422

Readme

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