Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · big

Docs

std/big

On this page

Index

Types

BigInt

struct BigInt {
  key: List[Int]
} derive Eq, Ord, Hash

An integer of any size. key encodes it so that the derived order is the numeric one: zero is [0]; a number of n 64 bit limbs is n (-n when negative), then its limbs from the most significant, each xor the sign bit (xor 2^63 - 1 when negative). Build one with the functions below, not a literal.

Source: std/big.xo:11

BigInt.add

fn (x: BigInt) add(y: BigInt) -> BigInt

Source: std/big.xo:31

BigInt.sub

fn (x: BigInt) sub(y: BigInt) -> BigInt

Source: std/big.xo:32

BigInt.mul

fn (x: BigInt) mul(y: BigInt) -> BigInt

Source: std/big.xo:33

BigInt.quo

fn (x: BigInt) quo(y: BigInt) -> BigInt

x / y truncated toward zero (Go’s Quo); a fault when y is 0.

Source: std/big.xo:35

BigInt.rem

fn (x: BigInt) rem(y: BigInt) -> BigInt

x - y * x.quo(y), with x’s sign (Go’s Rem).

Source: std/big.xo:37

BigInt.quo_rem

fn (x: BigInt) quo_rem(y: BigInt) -> (BigInt, BigInt)

Source: std/big.xo:38

BigInt.div

fn (x: BigInt) div(y: BigInt) -> BigInt

Euclidean division (Go’s Div): the remainder is never negative.

Source: std/big.xo:40

BigInt.mod

fn (x: BigInt) mod(y: BigInt) -> BigInt

x mod y in 0 to |y| - 1 (Go’s Mod).

Source: std/big.xo:42

BigInt.div_mod

fn (x: BigInt) div_mod(y: BigInt) -> (BigInt, BigInt)

Source: std/big.xo:43

BigInt.exp

fn (x: BigInt) exp(y: BigInt, m: BigInt? = None) -> BigInt

x to the y, mod |m| when m is given and not 0 (Go’s Exp): y <= 0 gives 1 without m; with m, a negative y uses x’s inverse mod m, a fault when there is none (Go returns nil).

Source: std/big.xo:47

BigInt.gcd

fn (x: BigInt) gcd(y: BigInt) -> BigInt

The greatest common divisor of |x| and |y|; 0 when both are 0.

Source: std/big.xo:49

BigInt.mod_inverse

fn (x: BigInt) mod_inverse(m: BigInt) -> BigInt?

x’s inverse mod m, None when gcd(x, m) is not 1 (Go’s ModInverse).

Source: std/big.xo:51

BigInt.sqrt

fn (x: BigInt) sqrt() -> BigInt

The floor of the square root; a fault when x < 0.

Source: std/big.xo:53

BigInt.neg

fn (x: BigInt) neg() -> BigInt

Source: std/big.xo:54

BigInt.abs

fn (x: BigInt) abs() -> BigInt

Source: std/big.xo:55

BigInt.sign

fn (x: BigInt) sign() -> Int

-1, 0, or 1.

Source: std/big.xo:57

BigInt.cmp

fn (x: BigInt) cmp(y: BigInt) -> Int

-1, 0, or 1 as x <, ==, > y (also the operators).

Source: std/big.xo:59

BigInt.cmp_abs

fn (x: BigInt) cmp_abs(y: BigInt) -> Int

Source: std/big.xo:60

BigInt.lsh

fn (x: BigInt) lsh(n: Int) -> BigInt

x * 2^n; a fault when n < 0.

Source: std/big.xo:62

BigInt.rsh

fn (x: BigInt) rsh(n: Int) -> BigInt

x / 2^n rounded toward negative infinity (Go’s Rsh); a fault when n < 0.

Source: std/big.xo:64

BigInt.and

fn (x: BigInt) and(y: BigInt) -> BigInt

The bitwise operations on two’s complement, as Go’s And, Or, Xor, AndNot, Not.

Source: std/big.xo:67

BigInt.or

fn (x: BigInt) or(y: BigInt) -> BigInt

Source: std/big.xo:68

BigInt.xor

fn (x: BigInt) xor(y: BigInt) -> BigInt

Source: std/big.xo:69

BigInt.and_not

fn (x: BigInt) and_not(y: BigInt) -> BigInt

Source: std/big.xo:70

BigInt.not

fn (x: BigInt) not() -> BigInt

Source: std/big.xo:71

BigInt.bit

fn (x: BigInt) bit(i: Int) -> Int

Bit i of x’s two’s complement (0 or 1); a fault when i < 0.

Source: std/big.xo:73

BigInt.bit_len

fn (x: BigInt) bit_len() -> Int

The length of |x| in bits; 0 for 0.

Source: std/big.xo:75

BigInt.trailing_zeros

fn (x: BigInt) trailing_zeros() -> Int

The number of zero bits below the lowest one of |x|; 0 for 0.

Source: std/big.xo:77

BigInt.probably_prime

fn (x: BigInt) probably_prime(n: Int) -> Bool

Whether x is probably prime: n Miller-Rabin rounds with Go’s pseudorandom bases plus a Baillie-PSW test (Go’s ProbablyPrime); a fault when n < 0.

Source: std/big.xo:81

BigInt.text

fn (x: BigInt) text(base: Int = 10) -> Str

x in base 2 to 62: digits, then a to z, then A to Z (Go’s Text); a fault for another base.

Source: std/big.xo:84

BigInt.bytes

fn (x: BigInt) bytes() -> Bytes

|x| big-endian, no leading zeros (Go’s Bytes).

Source: std/big.xo:86

BigInt.fill_bytes

fn (x: BigInt) fill_bytes(len: Int) -> Bytes

|x| big-endian in len bytes, zeros first; a fault when it does not fit (Go’s FillBytes).

Source: std/big.xo:89

BigInt.to_int

fn (x: BigInt) to_int() -> Int?

x as an Int, None when it does not fit (Go’s IsInt64 and Int64).

Source: std/big.xo:91

BigInt.to_float

fn (x: BigInt) to_float() -> Float

The Float nearest x (Go’s Float64; out of range is +Inf or -Inf).

Source: std/big.xo:93

BigInt.display

fn (x: BigInt) display() -> Str

x in base 10.

Source: std/big.xo:95

BigInt.debug

fn (x: BigInt) debug() -> Str

Source: std/big.xo:96

BigErr

struct BigErr {
  msg: Str
}

invalid number "12x" in base 10, invalid base 63 (Go’s SetString reports only false).

Source: std/big.xo:17

BigErr.display

fn (e: BigErr) display() -> Str

Source: std/big.xo:21

Functions

from_int

fn from_int(n: Int) -> BigInt

n (Go’s NewInt).

Source: std/big.xo:24

parse

fn parse(s: Str, base: Int = 10) -> BigInt ! BigErr

The number s in base 2 to 62 (Go’s SetString): an optional sign, then digits; base 0 reads a 0b, 0o, 0, or 0x prefix and _ between digits.

Source: std/big.xo:27

from_bytes

fn from_bytes(b: Bytes) -> BigInt

The unsigned big-endian number b (Go’s SetBytes).

Source: std/big.xo:29

↑ index