Integers
Integer wraps a JavaScript BigInt: whole numbers of any size, with arithmetic that promotes to fractions or intervals only when it has to.
Construction
The constructor and Integer.from accept a safe integer number, a decimal string, a bigint or another Integer. A number beyond has already lost digits before Core sees it, so it is rejected: pass a string or a bigint instead.
import { Integer, Rational } from "@ratmath/core";
const a = new Integer("9007199254740993"); // 2^53 + 1, exact
a.value; // 9007199254740993n
Integer.from(12n).toString(); // "12"
Integer.fromRational(new Rational(8, 4)); // Integer 2 (throws for 3/2)
Three different limits are worth keeping apart. A JavaScript number is exact only up to , which is why such inputs are refused. A bigint has no limit in the language, but each engine has one: some Bun releases refuse a BigInt above bits (about 315,000 digits) with a RangeError, where Node goes much further, and an intermediate value can cross that line even when the final answer is small. And Shelf libraries set their own budgets for work that grows quickly, such as exact orbits of the logistic map, whose denominators roughly double in length each step, and refuse with a message rather than run out.
Division stays exact
Dividing integers gives an Integer when the division is exact and a Rational otherwise; nothing is truncated unless you ask for it. There are two remainders, because languages disagree about negative numbers:
modulois JavaScript's%: the remainder has the sign of the dividend, .floorDivideandfloorModuloround the quotient down, so the remainder has the sign of the divisor: , .divmodreturns both.
new Integer(8).divide(new Integer(4)); // Integer 2
new Integer(7).divide(new Integer(2)); // Rational 7/2
new Integer(-7).modulo(new Integer(2)); // -1
new Integer(-7).floorModulo(new Integer(2)); // 1
new Integer(-7).divmod(new Integer(2)); // [-4, 1]
Powers
pow(n) is exact for every integer exponent: is a Rational. E(k) multiplies by , the exact form of scientific notation, so new Integer(125).E(-2) is . throws, as does to a negative power: Core and RiX agree that has no single value worth returning.
Number theory
gcd uses Euclid's algorithm, , and is always nonnegative; , and 0 if either is 0. factorial gives and doubleFactorial gives , with . bitLength is the number of bits of , useful for judging the cost of the next operation.
new Integer(84).gcd(new Integer(30)); // 6
new Integer(12).lcm(new Integer(18)); // 36
new Integer(20).factorial(); // 2432902008176640000
new Integer(7).doubleFactorial(); // 105 = 7·5·3·1
new Integer(255).bitLength(); // 8
Digits and place value
digits(base = 10) lists the digits of , most significant first. digitAt(k) is the digit in place (0 is the ones place) and placeValue(k) is its value with the sign of : the 8 in 486 is worth 80. round, floor, ceil and trunc return the integer itself, so code can call them on any Rational or Integer.
new Integer(486).digits(); // [4, 8, 6]
new Integer(486).placeValue(1); // 80
new Integer(10).digits(2); // [1, 0, 1, 0]
Output
toString(radix) takes a radix from 2 to 36 or a BaseSystem; toBase(system) uses any digit alphabet (see Bases). toNumber() crosses into floating point and can lose precision.
@ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.