bigfx
BigFX
is a JavaScript decimal fixed-point number library with unlimited size, based on the native BigInt
. It supports up to 15 digits of fractional precision, and is internally stored as a BigInt
scaled by a factor of 1e+15
. BigFX
can work on both Node.js and the browser.
Installation
Using NPM:
npm install bigfx
On the browser:
<script src="https://raw.githubusercontent.com/nirvanasupermind/bigfx/main/bigfx.min.js" type="text/javascript"></script>
Example
var BigFX = require("bigfx");
console.log(new BigFX(36.9).add(45).mul(94).toFixed(6)); // 7698.600000
console.log(new BigFX(9007199254740991n).add(2).toString()); // 9007199254740993
console.log(BigFX.PI.neg().cos().toString()); // -1
API
-
constructor()
- Creates aBigFX
object equal to 0 -
constructor(number)
- Creates aBigFX
object from a number (precision may be lost) -
constructor(bigint)
- Creates aBigFX
object from aBigInt
-
constructor(bigfx)
- Creates aBigFX
object from anotherBigFX
object -
clone()
- Returns a clone of aBigFX
object -
neg()
- Returns negation of aBigFX
object -
add(other)
- Returns addition of twoBigFX
objects (convertsother
if needed) -
sub(other)
- Returns subtraction of twoBigFX
objects (convertsother
if needed) -
mul(other)
- Returns multiplication of twoBigFX
objects (convertsother
if needed) -
div(other)
- Returns division of twoBigFX
objects (convertsother
if needed) -
mod(other)
- Returns modulo of twoBigFX
objects (convertsother
if needed) -
and(other)
- Returns bitwise AND of twoBigFX
objects (convertsother
if needed, only works if both arguments are integers) -
or(other)
- Returns bitwise OR of twoBigFX
objects (convertsother
if needed, only works if both arguments are integers) -
xor(other)
- Returns bitwise XOR of twoBigFX
objects (convertsother
if needed, only works if both arguments are integers) -
shl(other)
- Returns left-shift of twoBigFX
objects (convertsother
if needed, only works ifother
is an integer) -
shr(other)
- Returns right-shift of twoBigFX
objects (convertsother
if needed, only works ifother
is an integer) -
exp()
- Returns e raised to the power of aBigFX
object -
log()
- Returns natural logarithm of aBigFX
object -
pow(other)
- Returns exponentiation of twoBigFX
objects (convertsother
if needed) -
sqrt()
- Returns the square root of aBigFX
object -
sin()
- Returns the sine of aBigFX
object -
cos()
- Returns the cosine of aBigFX
object -
tan()
- Returns the tangent of aBigFX
object -
lt(other)
- Checks if aBigFX
object is less than anotherBigFX
object -
le(other)
- Checks if aBigFX
object is less than or equal to anotherBigFX
object -
gt(other)
- Checks if aBigFX
object is greater than anotherBigFX
object -
ge(other)
- Checks if aBigFX
object is greater than or equal to anotherBigFX
object -
eq(other)
- Checks if aBigFX
object is equal to anotherBigFX
object -
ne(other)
- Checks if aBigFX
object is not equal to anotherBigFX
object -
toNumber()
- Converts aBigFX
object to a number -
toBigInt()
- Converts aBigFX
object to aBigInt
-
toScaledBigInt()
- Converts aBigFX
object to aBigInt
scaled up by a factor of1e+15
(the internal representation) -
toString(radix = 10)
- Converts aBigFX
object to a string in the specified radix -
toFixed(fractionDigits = 0)
- Converts aBigFX
object to a string with the specified number of fraction digits -
toExponential()
- Converts aBigFX
object to a string in scientific notation format -
static ZERO
- ABigFX
object with a value of 0 -
static ONE
- ABigFX
object with a value of 1 -
static PI
- ABigFX
object with a value of π -
static E
- ABigFX
object with a value of e -
static random()
- Creates aBigFX
object with a pseudo-random value between 0 and 1