Skip to content

BigMathJS

Calculus_is_fun edited this page Aug 28, 2024 · 5 revisions

BigMathJS

BigMathJS is an object that has some functions used internally. But you can use them too! All functions are compatible with BigInts.

Functions

abs

A version of Math.abs(); that accepts BigInts

BigMathJS.abs(-1n); // 1n

Math.abs(-1n); // Cannot convert a BigInt value to a number

defactor

divides the second argument into the first argument until it can't do the division, then returns the remainder.

BigMathJS.defactor(128n, 2n); // 1

BigMathJS.defactor(128n, 3n); // 128

factorial

returns a BigInt equating to the argument factorial.

BigMathJS.factorial(7n); // 5040n

BigMathJS.factorial(5n); // 120n

gcd

Takes 2 arguments and returns the greatest integer that divides into them both.

BigMathJS.gcd(5n,3n); // 1n

BigMathJS.gcd(10n,4n); // 2n

lcm

Takes 2 arguments and returns the smallest integer that can be divided by them both.

BigMathJS.lcm(5n, 10n); // 10n

BigMathJS.lcm(20n, 3n); // 60n

mod

Like a % b, but always returns an integer between 0 to b-1

BigMathJS.mod(-1n, 10n); // 9n

BigMathJS.mod(-10, 3n) // 2n

sign

returns 1n if argument > 0n, -1n when < 0n, and 0n when == 0n

BigMathJS.sign(-10n); // -1n

BigMathJS.sign(5241n); // 1n

Clone this wiki locally