Nanobot: Polygonal Mascot

# npm

Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

## bigfloat-esnext

1.0.2 • Public • Published

# bigfloat-esnext

A library for arbitrary precision decimal floating point arithmetic that can exactly represent all decimal fractions, unlike JavaScript's number data type which is 64-bit binary floating point.

Based on the original work by Douglas Crockford. This implementation is built upon ES native bigints.

It also understands scientific e-notation:

This library provides a set of functions for basic operations, and an evaluate() function that makes bigfloat operations behind the scenes. The first operation shown above can also be performed by making use of the other provided functions like this:

You can also use the familiar Decimal.js API (partial implementation as of now):

# Importing the bigfloat module

CommonJS:

ESModules or TS:

# The bigfloat object

The coefficient is a bigint that contains all of the digits that make up the number. The exponent is a number that indicates where to place the decimal point. This bigfloat object represents the decimal value 52.2299

# Change precision

This function only takes negative integers. The default precision is -4.

# evaluate(expression, precision)

This function takes an expression in string form, and a negative integer for precision (default is -4) and returns a string:

Or a boolean:

The tokens that make up the expression can be:

• Parenthesis: (,)
• Number: Decimal form or scientific e-notation
• Operator: Arithmetic +,-,/,*,** Relational ===,==,!==,!=,<,>,<=,>=

It would be nice to have a transpiler that replaces JavaScript numbers and operators for bigfloat function calls, but it seemed to me very convenient to have this functionality available at runtime.

# BigFloat(number) / make(number)

This function takes a number in a string or number form and returns a bigfloat object.

# string(bigfloat)

This function takes a bigfloat object and returns a string containing the decimal representation of the number. The conversion is exact.

# Changelog

1.0.1

1.0.0

• First version

### Install

`npm i bigfloat-esnext`

3

1.0.2

MIT

109 kB

27

### Repository

github.com/davidmartinez10/bigfloat-esnext