-
Notifications
You must be signed in to change notification settings - Fork 0
Rational
These arguments must be BigInts
The numerator of the fraction.
the denominator of the fraction, can't be less than 1. (default is 1)
let fraction1 = new Rational(4n); // fraction1 is 4/1
let fraction2 = new Rational(5n, 3n); // fraction2 is 5/3
most of these mutate this, exceptions will be noted
adds argument to this
let frac = new Rational(1n, 8n);
frac.add(3n); // frac is now 25/8
frac.add(new Rational(1n, 2n)); // frac is now 29/8
increases this to the next integer, or does nothing if already an integer
let frac = new Rational(1n, 100n);
let int = new Rational(2n);
frac.ceiling(); // frac is now 1/1
int.ceiling(); // int is still 2/1
this does not mutate this
returns copy of this, potentially better than structuredClone();
let frac = new Rational(3n, 2n);
let clone = frac.clone();
frac.sub(1n);
console.log(frac.toString()); // "1/2"
console.log(clone.toString()); // "3/2"
this does not mutate this
returns -1n, 0n, or 1n if this is <, ==, or > argument respectively
let frac = new Rational(1n, 3n);
console.log(frac.compare(1n)); // -1n
console.log(frac.compare(new Rational(-1n, 2n))); // 1n
divides argument into this
let frac = new Rational(4n);
frac.div(5n); // frac is now 4/5
frac.div(new Rational(2n, 3n)) // frac is now 6/5
decreases this to the next integer, or does nothing if already an integer
let frac = new Rational(99n, 100n);
let int = new Rational(2n);
frac.floor(); // frac is now 0/1
int.floor(); // int is still 2/1
rounds this toward zero to the next integer, or does nothing if already an integer
let pFrac = new Rational(199n, 100n);
let nFrac = new Rational(-25n, 8n);
let int = new Rational(2n);
pFrac.integer(); // pFrac is now 1/1
nFrac.integer(); // nFrac is now -3/1
int.integer(); // int is still 2/1
swaps the numerator and the denominator
let frac = new Rational(3n, 5n);
frac.inverse(); // frac is now 5/3
multiplies argument with this
let frac = new Rational(1n, 21n);
frac.mult(3); // frac is now 1/7
frac.mult(new Rational(3n, 2n)); // frac is now 3/14
subtracts argument from this
let frac = new Rational(3n);
frac.sub(1n); // frac is now 2/1
frac.sub(new Rational(1n, 5n)); // frac is now 9/5
this does not mutate this
returns a decimal expansion of a string, with the recurring section incased in square brackets.
- decimalLength (BigInt): how many digits appear after the decimal separator, negative numbers are treated as infinity, defaults to 3
- base (BigInt): the base of the expansion, must be in the range 2-36, defaults to 10
- separator (String): a string that separates the integer and decimal, defaults to "."
let frac = new Rational(1n, 7n);
console.log(frac.toDecimal()); // "0.142"
console.log(frac.toDecimal(-1n)); // "0.[142857]"
console.log(frac.toDecimal(-1n, 7n, ",")); // "0,1[0]"
this does not mutate this
returns a LaTeX code corresponding to this
let frac = new Rational(1n, 5n);
console.log(frac.toLatex()); // "\frac{1}{5}"
this does not mutate this
returns a string with the numerator and denominator separated by /
by default hides denominator when it's 1
let frac = new Rational(5n/7n);
console.log(frac.toString()); // "5/7"
let int = new Rational(4n);
console.log(int.toString()) // "4"
console.log(int.toString(false)); // "4/1"