Grav v1.14.0

bigint

import "std/types" (included in import "std") · namespace std.types

Whole numbers of any size: cryptography, exact factorials and combinatorics, amounts in the smallest unit past 2^63, and IDs from other systems. A BigInt is immutable, and the operators work on it as on a long, with whole numbers mixing in on the right.

grav
import "std"

fn main() {
    let a = BigInt.parse("123456789012345678901234567890")
    print(a * a + BigInt.one())
    print(BigInt.of(2).pow(128))
    print(BigInt.factorial(25))
    print(BigInt.binomial(60, 30))
    print("${a / 1000} ${a % 97} ${a > BigInt.of(1)}")
    print("${BigInt.of(-7) / BigInt.of(2)} ${BigInt.of(-7) % BigInt.of(2)} ${BigInt.of(-7).mod(BigInt.of(2))}")
    print("${BigInt.parse("ff_ff", 16)} ${BigInt.of(255).toString(2)}")
    print(BigInt.of(7).modPow(BigInt.of(560), BigInt.of(561)))
    print(BigInt.parse("170141183460469231731687303715884105727").isProbablePrime())
    print(BigInt.of(10).pow(40).sqrt())
}
Output
15241578753238836750495351562536198787501905199875019052101
340282366920938463463374607431768211456
15511210043330985984000000
118264581564861424
123456789012345678901234567 52 true
-3 -1 1
65535 11111111
1
true
100000000000000000000
  • Arithmetic: + - * / % (and plus, minus, times, div, rem, divRem for both at once), negate, abs, pow(e), sqrt (the integer root), gcd. / and % truncate toward zero and the remainder takes the dividend's sign, as for long; mod(m) is never negative. Division by zero is an ArithmeticError.
  • Modular arithmetic: modPow(e, m) (a negative e uses the inverse), modInverse(m), and isProbablePrime(rounds) (Miller–Rabin; exact below about 3.3 × 10^24 with the default 12 rounds).
  • Bits: shiftLeft, shiftRight (rounding toward negative infinity, as >>), bitLength, testBit, and and, or, xor for non-negative values.
  • Conversions: BigInt.of(long), parse(text, base) (bases 2 to 36, an optional sign, _ between digits), tryParse, toString(base), toHex, toLong and toInt (an ArithmeticError when the value does not fit; fitsLong() asks first), and toDouble.
  • Speed: the digits are 32-bit limbs handled in C. Multiplication is schoolbook below 40 limbs and Karatsuba above, division is Knuth's algorithm D, factorial and binomial multiply in a balanced product tree, and modPow reduces after every step instead of building the huge power.

API

class BigInt

An integer of any size.

Constructors

  • new BigInt()

Static members

  • static fn of(v: long) -> BigInt — The value of a long.
  • static fn zero() -> BigInt — 0 and 1.
  • static fn one() -> BigInt
  • static fn parse(text: string, base: int = 10) -> BigInt — Parses digits in base (2 to 36; an optional sign; '_' between digits allowed); ValueError when the text is not a number.
  • static fn tryParse(text: string, base: int = 10) -> BigInt? — parse, or null when the text is not a number.
  • static fn factorial(n: int) -> BigInt — n! (a product tree: fast for large n).
  • static fn binomial(n: int, k: int) -> BigInt — n choose k.

Methods

  • fn sign() -> int — -1, 0 or 1.
  • fn isZero() -> bool
  • fn isNegative() -> bool
  • fn isEven() -> bool
  • fn fitsLong() -> bool — Whether it fits in a long.
  • fn toLong() -> long — The value as a long; ArithmeticError when it does not fit.
  • fn toInt() -> int — The value as an int; ArithmeticError when it does not fit.
  • fn toDouble() -> double — The nearest double (infinity past its range).
  • fn toString() -> string — Decimal digits.
  • fn toString(base: int) -> string — Digits in base (2 to 36, lowercase letters).
  • fn toHex() -> string — Hex digits (no prefix; a '-' for negative values).
  • fn bitLength() -> int — The number of bits in the magnitude (0 for 0).
  • fn testBit(i: int) -> bool — Whether bit i of the magnitude is set.
  • fn hash() -> long — A hash of the value (equal values hash alike).
  • fn plus(o: BigInt) -> BigInt
  • fn minus(o: BigInt) -> BigInt
  • fn times(o: BigInt) -> BigInt
  • fn div(o: BigInt) -> BigInt — The quotient, toward zero; ArithmeticError for division by zero.
  • fn rem(o: BigInt) -> BigInt — The remainder of div (the dividend's sign).
  • fn divRem(o: BigInt) -> Array<BigInt> — [quotient, remainder] from one division.
  • fn mod(m: BigInt) -> BigInt — The remainder in [0, |m|): modular arithmetic's mod.
  • fn negate() -> BigInt
  • fn abs() -> BigInt
  • fn pow(e: int) -> BigInt — self to the power e (e >= 0).
  • fn modPow(e: BigInt, m: BigInt) -> BigInt — self^e mod m (e >= 0, m > 0), without the huge intermediate power.
  • fn modInverse(m: BigInt) -> BigInt — x with self * x ≡ 1 (mod m); ArithmeticError when there is none.
  • fn gcd(o: BigInt) -> BigInt — The greatest common divisor (never negative).
  • fn sqrt() -> BigInt — The integer square root: the largest r with r*r <= self (self >= 0).
  • fn isProbablePrime(rounds: int = 12) -> bool — Miller–Rabin with the first rounds primes as witnesses (12 is exact below 3.3 * 10^24; beyond that a composite passes with odds under 4^-rounds).
  • fn shiftLeft(k: int) -> BigInt — self * 2^k.
  • fn shiftRight(k: int) -> BigInt — self / 2^k, rounded toward negative infinity (as >> on a long).
  • fn and(o: BigInt) -> BigInt — Bitwise and, or and xor of non-negative values; ValueError for negative ones.
  • fn or(o: BigInt) -> BigInt
  • fn xor(o: BigInt) -> BigInt
  • fn compareTo(o: BigInt) -> int — Negative, zero or positive as self is below, equal to or above o.
  • fn equals(o: BigInt) -> bool
  • fn max(o: BigInt) -> BigInt
  • fn min(o: BigInt) -> BigInt
  • fn operator+(o: BigInt) -> BigInt
  • fn operator-(o: BigInt) -> BigInt
  • fn operator*(o: BigInt) -> BigInt
  • fn operator/(o: BigInt) -> BigInt
  • fn operator%(o: BigInt) -> BigInt
  • fn operator+(v: long) -> BigInt
  • fn operator-(v: long) -> BigInt
  • fn operator*(v: long) -> BigInt
  • fn operator/(v: long) -> BigInt
  • fn operator%(v: long) -> BigInt
  • fn operator==(o: BigInt) -> bool
  • fn operator<(o: BigInt) -> bool
  • fn operator<=(o: BigInt) -> bool
  • fn operator>(o: BigInt) -> bool
  • fn operator>=(o: BigInt) -> bool
  • fn operator==(v: long) -> bool
  • fn operator<(v: long) -> bool
  • fn operator<=(v: long) -> bool
  • fn operator>(v: long) -> bool
  • fn operator>=(v: long) -> bool

Edit this page on GitHub