Skip to content

Math Reference

Coverage License NPM Version Open Issues Size

๐Ÿงฎ Precise basic math operations. Using this library, adding 0.1 to 0.2 will give you 0.3, which might sound like nothing, but thatโ€™s not the default behavior of JavaScript.

Usage

๐Ÿ“ฆ Node

Install @lou.codes/math as a dependency:

Terminal window
1
pnpm add @lou.codes/math
2
# or
3
npm install @lou.codes/math
4
# or
5
yarn add @lou.codes/math

Import it and use it:

1
import { chain } from "@lou.codes/math";
2
3
chain(0.2).add(0.1); // 0.3 ๐Ÿช„

๐Ÿฆ• Deno

Import @lou.codes/math using the npm: prefix, and use it directly:

1
import { chain } from "npm:@lou.codes/math";
2
3
chain(0.2).add(0.1); // 0.3 ๐Ÿช„

๐ŸŒŽ Browser

Import @lou.codes/math using esm.sh, and use it directly:

1
<script type="module">
2
import { chain } from "https://esm.sh/@lou.codes/math";
3
4
chain(0.2).add(0.1); // 0.3 ๐Ÿช„
5
</script>

Internal

Precise

1
type Precise: readonly [bigint, bigint];

Type to precisely represent a number as a tuple [base, exponent].

View source


bigIntMin()

1
function bigIntMin(...bigints: readonly bigint[]): bigint;

Like Math.min but for bigint values.

Parameters

ParameterType
โ€ฆbigintsreadonly bigint[]

Returns

bigint

Minimum value of the bigint array.

Example

1
bigIntMin(5n, 1n, 10n); // 1n

See

Math.min

View source


createPrecise()

1
function createPrecise(base: bigint, exponent: bigint): Precise;

Takes a base and exponent and normalizes it returning a Precise.

Parameters

ParameterTypeDescription
basebigintBase of the Precise.
exponentbigintExponent of the Precise.

Returns

Precise

A normalized Precise value.

Example

1
createPrecise(13n); // [13n]
2
createPrecise(13n, -1n); // [13n, -1n]
3
createPrecise(1300n, 0n); // [13n, 2n]

See

Precise

View source


numberToPrecise()

1
function numberToPrecise(number: number): Precise;

Turns a number into a Precise.

Parameters

ParameterTypeDescription
numbernumberNumber to convert.

Returns

Precise

A Precise representation of the given number.

Example

1
numberToPrecise(13); // [13n]
2
numberToPrecise(1.3); // [13n, -1n]
3
numberToPrecise(1300); // [13n, 2n]

See

createPrecise

View source


pipe()

1
function pipe<PreciseOperation>(
2
preciseOperation: PreciseOperation,
3
): (right: number) => (left: number) => number;

Turns a Precise operation into a number operation.

Type parameters

Type parameter
PreciseOperation extends (โ€ฆright: Precise) => (โ€ฆleft: Precise) => Precise

Parameters

ParameterTypeDescription
preciseOperationPreciseOperationPrecise operation function.

Returns

Function

Number to number operation generated from preciseOperation.

Parameters
ParameterType
rightnumber
Returns

Function

Parameters
ParameterType
leftnumber
Returns

number

Example

1
const add = pipe(preciseAdd);
2
3
add(0.1)(0.2); // 0.3

See

View source


preciseAdd()

1
function preciseAdd(
2
addendRightBase: bigint,
3
addendRightExponent: bigint,
4
): (addendLeftBase: bigint, addendLeftExponent: bigint) => Precise;

Curried add operation using the internal Precise type.

Parameters

ParameterTypeDescription
addendRightBasebigintAddend base to use in the right side of the addition.
addendRightExponentbigintAddend exponent to use in the right side of the addition.

Returns

Function

Curried function with addendRightBase and addendRightExponent in context.

Parameters
ParameterType
addendLeftBasebigint
addendLeftExponentbigint
Returns

Precise

Example

1
const addDot2 = preciseAdd(2n);
2
3
addDot2(1n, -1n); // [3n, -1n]

See

View source


preciseDivide()

1
function preciseDivide(
2
divisorBase: bigint,
3
divisorExponent: bigint,
4
): (dividendBase: bigint, dividendExponent?: bigint) => Precise;

Curried divide operation using the internal Precise type.

Parameters

ParameterTypeDescription
divisorBasebigintDivisor base to use in the division.
divisorExponentbigintDivisor exponent to use in the division.

Returns

Function

Curried function with divisorBase and divisorExponent in context.

Parameters
ParameterType
dividendBasebigint
dividendExponent?bigint
Returns

Precise

Example

1
const half = preciseDivide(2n);
2
3
half(1n); // [5n, -1n]

See

View source


preciseMultiply()

1
function preciseMultiply(
2
multiplierBase: bigint,
3
multiplierExponent: bigint,
4
): (multiplicandBase: bigint, multiplicandExponent: bigint) => Precise;

Curried multiply operation using the internal Precise type.

Parameters

ParameterTypeDescription
multiplierBasebigintMultiplier base to use in the multiplication.
multiplierExponentbigintMultiplier exponent to use in the multiplication.

Returns

Function

Curried function with multiplierBase and multiplierExponent in context.

Parameters
ParameterType
multiplicandBasebigint
multiplicandExponentbigint
Returns

Precise

Example

1
const double = preciseMultiply(2n);
2
3
double(65n, -1n); // [13n]

See

View source


preciseSubtract()

1
function preciseSubtract(
2
subtrahendBase: bigint,
3
subtrahendExponent: bigint,
4
): (minuendBase: bigint, minuendExponent?: bigint) => Precise;

Curried subtract operation using the internal Precise type.

Parameters

ParameterTypeDescription
subtrahendBasebigintSubtrahend base to use in the subtraction.
subtrahendExponentbigintSubtrahend exponent to use in the subtraction.

Returns

Function

Curried function with subtrahendBase and subtrahendExponent in context.

Parameters
ParameterType
minuendBasebigint
minuendExponent?bigint
Returns

Precise

Example

1
const previous = preciseSubtract(1n);
2
3
previous(14n); // [13n]

See

View source


preciseToNumber()

1
function preciseToNumber(base: bigint, exponent: bigint): number;

Turns a Precise into a number.

Parameters

ParameterType
basebigint
exponentbigint

Returns

number

Number represented by the passed precise value.

Example

1
preciseToNumber(3n, -1n); // 0.3

See

Precise

View source

Operations

add()

1
function add(addendRight: number): (addendLeft: number) => number;

Curried add operation using pipe with preciseAdd.

Parameters

ParameterTypeDescription
addendRightnumberAddend value to be on the right side.

Returns

Function

Curried function with addendRight in context.

Parameters
ParameterType
addendLeftnumber
Returns

number

Example

1
const addDot2 = add(2);
2
3
addDot2(0.1); // 0.3

See

View source


chain()

1
function chain(value: number): Readonly<object>;

A chainable set of operations.

Parameters

ParameterTypeDescription
valuenumberValue to run operations on.

Returns

Readonly<object>

An object with divideBy, minus, plus and times methods and a value property.

MemberTypeValueDescription
dividedBy(divisor: number) => Readonly<{ dividedBy: (divisor: number) => Readonly<โ€ฆ>; minus: (subtrahend: number) => Readonly<โ€ฆ>; plus: (addend: number) => Readonly<โ€ฆ>; times: (multiplier: number) => Readonly<โ€ฆ>; value: number; }>โ€ฆDivide previous value in chain by the given divisor
minus(subtrahend: number) => Readonly<{ dividedBy: (divisor: number) => Readonly<โ€ฆ>; minus: (subtrahend: number) => Readonly<โ€ฆ>; plus: (addend: number) => Readonly<โ€ฆ>; times: (multiplier: number) => Readonly<โ€ฆ>; value: number; }>โ€ฆSubtracts given subtrahend to the current value in the chain.
plus(addend: number) => Readonly<{ dividedBy: (divisor: number) => Readonly<โ€ฆ>; minus: (subtrahend: number) => Readonly<โ€ฆ>; plus: (addend: number) => Readonly<โ€ฆ>; times: (multiplier: number) => Readonly<โ€ฆ>; value: number; }>โ€ฆAdds given addend to the current value in the chain.
times(multiplier: number) => Readonly<{ dividedBy: (divisor: number) => Readonly<โ€ฆ>; minus: (subtrahend: number) => Readonly<โ€ฆ>; plus: (addend: number) => Readonly<โ€ฆ>; times: (multiplier: number) => Readonly<โ€ฆ>; value: number; }>โ€ฆMultiplies previous value in chain times the given multiplier
valuenumber--

Example

1
chain(0.1).add(0.2).value; // 0.3
2
chain(0.7).plus(0.3).dividedBy(4).times(2).minus(0.2).value; // 0.3

See

View source


divide()

1
function divide(divisor: number): (dividend: number) => number;

Curried divide operation using pipe with preciseDivide.

Parameters

ParameterTypeDescription
divisornumberDivisor to be used in the division.

Returns

Function

Curried function with divisor in context.

Parameters
ParameterType
dividendnumber
Returns

number

Example

1
const half = divide(2);
2
3
half(1); // 0.5

See

View source


multiply()

1
function multiply(multiplier: number): (multiplicand: number) => number;

Curried multiply operation using pipe with preciseMultiply.

Parameters

ParameterTypeDescription
multipliernumberMultiplier value to be used in the multiplication.

Returns

Function

Curried function with multiplier in context.

Parameters
ParameterType
multiplicandnumber
Returns

number

Example

1
const double = multiply(2);
2
3
double(6.5); // 13

See

View source


subtract()

1
function subtract(subtrahend: number): (minuend: number) => number;

Curried subtract operation using pipe with preciseSubtract.

Parameters

ParameterTypeDescription
subtrahendnumberSubtrahend value to be used in the subtraction.

Returns

Function

Curried function with subtrahend in context.

Parameters
ParameterType
minuendnumber
Returns

number

Example

1
const previous = subtract(1);
2
3
previous(14); // 13

See

View source