Math Reference
๐งฎ 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:
Import it and use it:
๐ฆ Deno
Import @lou.codes/math
using the npm:
prefix, and use it directly:
๐ Browser
Import @lou.codes/math
using esm.sh, and use it directly:
Useful links
- ๐ Documentation: TypeDoc generated documentation.
- โณ Changelog: List of changes between versions.
- โ Tests Coverage: Coveralls page with tests coverage.
Internal
MaybeInfinity
Dividing by zero would yield Infinity
.
Precise
Type to precisely represent a number as a tuple [base, exponent]
. It can be
[Infinity]
.
bigIntMin()
Like Math.min
but for bigint
values.
Parameters
Parameter | Type |
---|---|
โฆbigints | readonly bigint [] |
Returns
bigint
Minimum value of the bigint
array.
Example
See
createPrecise()
createPrecise(base, exponent)
Takes a base
and exponent
and normalizes it returning a
Precise.
Parameters
Parameter | Type | Description |
---|---|---|
base | bigint | Base of the Precise. |
exponent ? | bigint | Exponent of the Precise. |
Returns
A normalized Precise value.
Example
See
createPrecise(base)
Takes a base
and exponent
and normalizes it returning a
Precise.
Parameters
Parameter | Type | Description |
---|---|---|
base | number | Base of the Precise. |
Returns
A normalized Precise value.
Example
See
numberToPrecise()
Turns a number
into a Precise.
Parameters
Parameter | Type | Description |
---|---|---|
number | number | Number to convert. |
Returns
A Precise representation of the given
number
.
Example
See
pipe()
Turns a Precise operation into a number operation.
Type parameters
Type parameter |
---|
PreciseOperation extends (โฆright : Precise ) => (โฆleft : Precise ) => Precise |
Parameters
Parameter | Type | Description |
---|---|---|
preciseOperation | PreciseOperation | Precise operation function. |
Returns
Function
Number to number operation generated from preciseOperation
.
Parameters
Parameter | Type |
---|---|
right | number |
Returns
Function
Parameters
Parameter | Type |
---|---|
left | number |
Returns
number
Example
See
preciseAdd()
Curried add operation using the internal Precise type.
Parameters
Parameter | Type | Description |
---|---|---|
addendRightBase | MaybeInfinity | Addend base to use in the right side of the addition. |
addendRightExponent | bigint | Addend exponent to use in the right side of the addition. |
Returns
Function
Curried function with addendRightBase
and addendRightExponent
in context.
Parameters
Parameter | Type |
---|---|
addendLeftBase | MaybeInfinity |
addendLeftExponent | bigint |
Returns
Example
See
preciseDivide()
Curried divide operation using the internal Precise type.
Parameters
Parameter | Type | Description |
---|---|---|
divisorBase | MaybeInfinity | Divisor base to use in the division. |
divisorExponent | bigint | Divisor exponent to use in the division. |
Returns
Function
Curried function with divisorBase
and divisorExponent
in context.
Parameters
Parameter | Type |
---|---|
dividendBase | MaybeInfinity |
dividendExponent ? | bigint |
Returns
Example
See
preciseMultiply()
Curried multiply operation using the internal Precise type.
Parameters
Parameter | Type | Description |
---|---|---|
multiplierBase | MaybeInfinity | Multiplier base to use in the multiplication. |
multiplierExponent | bigint | Multiplier exponent to use in the multiplication. |
Returns
Function
Curried function with multiplierBase
and multiplierExponent
in context.
Parameters
Parameter | Type |
---|---|
multiplicandBase | MaybeInfinity |
multiplicandExponent | bigint |
Returns
Example
See
preciseSubtract()
Curried subtract operation using the internal Precise type.
Parameters
Parameter | Type | Description |
---|---|---|
subtrahendBase | MaybeInfinity | Subtrahend base to use in the subtraction. |
subtrahendExponent | bigint | Subtrahend exponent to use in the subtraction. |
Returns
Function
Curried function with subtrahendBase
and subtrahendExponent
in context.
Parameters
Parameter | Type |
---|---|
minuendBase | MaybeInfinity |
minuendExponent ? | bigint |
Returns
Example
See
preciseToNumber()
Turns a Precise into a number
.
Parameters
Parameter | Type |
---|---|
base | number | bigint |
exponent | bigint |
Returns
number
Number represented by the passed precise
value.
Example
See
Operations
add()
Curried add operation using pipe with preciseAdd.
Parameters
Parameter | Type | Description |
---|---|---|
addendRight | number | Addend value to be on the right side. |
Returns
Function
Curried function with addendRight
in context.
Parameters
Parameter | Type |
---|---|
addendLeft | number |
Returns
number
Example
See
chain()
A chainable set of operations.
Parameters
Parameter | Type | Description |
---|---|---|
value | number | Value to run operations on. |
Returns
Readonly
<object
>
An object with divideBy
, minus
, plus
and times
methods and a value
property.
Member | Type | Value | Description |
---|---|---|---|
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 |
value | number | - | - |
Example
See
divide()
Curried divide operation using pipe with preciseDivide.
Parameters
Parameter | Type | Description |
---|---|---|
divisor | number | Divisor to be used in the division. |
Returns
Function
Curried function with divisor
in context.
Parameters
Parameter | Type |
---|---|
dividend | number |
Returns
number
Example
See
multiply()
Curried multiply operation using pipe with preciseMultiply.
Parameters
Parameter | Type | Description |
---|---|---|
multiplier | number | Multiplier value to be used in the multiplication. |
Returns
Function
Curried function with multiplier
in context.
Parameters
Parameter | Type |
---|---|
multiplicand | number |
Returns
number
Example
See
subtract()
Curried subtract operation using pipe with preciseSubtract.
Parameters
Parameter | Type | Description |
---|---|---|
subtrahend | number | Subtrahend value to be used in the subtraction. |
Returns
Function
Curried function with subtrahend
in context.
Parameters
Parameter | Type |
---|---|
minuend | number |
Returns
number