Topic

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 253−12^{53} - 1 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 253−12^{53} - 1, 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 2202^{20} 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:

  • modulo is JavaScript's %: the remainder has the sign of the dividend, −7 mod 2=−1-7 \bmod 2 = -1.
  • floorDivide and floorModulo round the quotient down, so the remainder has the sign of the divisor: q=⌊a/b⌋q = \lfloor a/b \rfloor, r=a−bqr = a - bq. divmod returns both.
a=b q+r,q=⌊ab⌋,0≤r<b  when b>0. a = b\,q + r, \qquad q = \left\lfloor \frac{a}{b} \right\rfloor, \qquad 0 \le r < b \ \text{ when } b > 0.
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: 2−3=182^{-3} = \tfrac18 is a Rational. E(k) multiplies by 10k10^k, the exact form of scientific notation, so new Integer(125).E(-2) is 54\tfrac54. 000^0 throws, as does 00 to a negative power: Core and RiX agree that 000^0 has no single value worth returning.

Number theory

gcd uses Euclid's algorithm, gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b), and is always nonnegative; lcm⁡(a,b)=∣ab∣/gcd⁡(a,b)\operatorname{lcm}(a,b) = |ab| / \gcd(a,b), and 0 if either is 0. factorial gives n!n! and doubleFactorial gives n!!=n(n−2)(n−4)⋯n!! = n(n-2)(n-4)\cdots, with 0!=0!!=10! = 0!! = 1. bitLength is the number of bits of ∣a∣|a|, 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 ∣n∣|n|, most significant first. digitAt(k) is the digit in place kk (0 is the ones place) and placeValue(k) is its value with the sign of nn: 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.

Try itEdit the code and run it with @ratmath/core in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs