An arbitrary-precision big integer along with an allocator which manages the memory.

Memory is allocated as needed to ensure operations never overflow. The range is bounded only by available memory.

Fields

allocator: Allocator,

Allocator used by the Managed when requesting memory.

limbs: []Limb,

Raw digits. These are:

  • Little-endian ordered
  • limbs.len >= 1
  • Zero is represent as Managed.len() == 1 with limbs[0] == 0.

Accessing limbs directly should be avoided.

metadata: usize,

High bit is the sign bit. If set, Managed is negative, else Managed is positive. The remaining bits represent the number of limbs used by Managed.

Functions

fn abs(self: *Managed) void

Make positive.

fn add(r: *Managed, a: *const Managed, b: *const Managed) Allocator.Error!void

r = a + b

r = a + b

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn addSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!void

r = a + b with 2s-complement saturating semantics.

r = a + b with 2s-complement saturating semantics.

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn addScalar(r: *Managed, a: *const Managed, scalar: anytype) Allocator.Error!void

r = a + scalar

r = a + scalar

r and a may be aliases.

Returns an error if memory could not be allocated.

fn addWrap(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!bool

r = a + b with 2s-complement wrapping semantics. Returns whether any overflow oc…

r = a + b with 2s-complement wrapping semantics. Returns whether any overflow occurred.

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn bitAnd(r: *Managed, a: *const Managed, b: *const Managed) !void

r = a & b

fn bitCountAbs(self: Managed) usize

Returns the number of bits required to represent the absolute value of an intege…

Returns the number of bits required to represent the absolute value of an integer.

fn bitCountTwosComp(self: Managed) usize

Returns the number of bits required to represent the integer in twos-complement …

Returns the number of bits required to represent the integer in twos-complement form.

If the integer is negative the value returned is the number of bits needed by a signed integer to represent the value. If positive the value is the number of bits for an unsigned integer. Any unsigned integer will fit in the signed integer with bitcount one greater than the returned value.

e.g. -127 returns 8 as it will fit in an i8. 127 returns 7 since it fits in a u7.

fn bitNotWrap(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void

r = ~a under 2s-complement wrapping semantics. r and a may alias.

fn bitOr(r: *Managed, a: *const Managed, b: *const Managed) !void

r = a | b

r = a | b

a and b are zero-extended to the longer of a or b.

fn bitXor(r: *Managed, a: *const Managed, b: *const Managed) !void

r = a ^ b

fn clone(other: Managed) !Managed

Returns a Managed with the same value. The returned Managed is a deep copy a…

Returns a Managed with the same value. The returned Managed is a deep copy and can be modified separately from the original, and its resources are managed separately from the original.

fn cloneWithDifferentAllocator(other: Managed, allocator: Allocator) !Managed

No documentation provided.

fn copy(self: *Managed, other: Const) !void

Copies the value of the integer to an existing Managed so that they both have …

Copies the value of the integer to an existing Managed so that they both have the same value. Extra memory will be allocated if the receiver does not have enough capacity.

fn deinit(self: *Managed) void

Frees all associated memory.

fn divFloor(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void

q = a / b (rem r)

q = a / b (rem r)

a / b are floored (rounded towards 0).

Returns an error if memory could not be allocated.

fn divTrunc(q: *Managed, r: *Managed, a: *const Managed, b: *const Managed) !void

q = a / b (rem r)

q = a / b (rem r)

a / b are truncated (rounded towards -inf).

Returns an error if memory could not be allocated.

fn dump(self: Managed) void

Debugging tool: prints the state to stderr.

fn ensureAddCapacity(r: *Managed, a: Const, b: Const) !void

No documentation provided.

fn ensureAddScalarCapacity(r: *Managed, a: Const, scalar: anytype) !void

No documentation provided.

fn ensureCapacity(self: *Managed, capacity: usize) !void

Ensures an Managed has enough space allocated for capacity limbs. If the Managed…

Ensures an Managed has enough space allocated for capacity limbs. If the Managed does not have sufficient capacity, the exact amount will be allocated. This occurs even if the requested capacity is only greater than the current capacity by one limb.

fn ensureMulCapacity(rma: *Managed, a: Const, b: Const) !void

No documentation provided.

fn ensureTwosCompCapacity(r: *Managed, bit_count: usize) !void

No documentation provided.

fn eql(a: Managed, b: Managed) bool

Returns true if a == b.

fn eqlAbs(a: Managed, b: Managed) bool

Returns true if |a| == |b|.

fn eqlZero(a: Managed) bool

Returns true if a == 0.

fn fits(self: Managed, comptime T: type) bool

Returns whether self can fit into an integer of the requested type.

fn fitsInTwosComp(self: Managed, signedness: Signedness, bit_count: usize) bool

No documentation provided.

fn format(self: Managed, comptime fmt: []const u8, options: std.fmt.FormatOptions, out_stream: anytype) !void

To allow std.fmt.format to work with Managed. If the integer is larger than…

To allow std.fmt.format to work with Managed. If the integer is larger than pow(2, 64 * @sizeOf(usize) * 8), this function will fail to print the string, printing "(BigInt)" instead of a number. This is because the rendering algorithm requires reversing a string, which requires O(N) memory. See toStringandtoStringAlloc` for a way to print big integers without failure.

fn gcd(rma: *Managed, x: *const Managed, y: *const Managed) !void

rma may alias x or y. x and y may alias each other.

rma may alias x or y. x and y may alias each other.

rma’s allocator is used for temporary storage to boost multiplication performance.

fn init(allocator: Allocator) !Managed

Creates a new Managed. default_capacity limbs will be allocated immediately….

Creates a new Managed. default_capacity limbs will be allocated immediately. The integer value after initializing is 0.

fn initCapacity(allocator: Allocator, capacity: usize) !Managed

Creates a new Managed with a specific capacity. If capacity < default_capacity t…

Creates a new Managed with a specific capacity. If capacity < default_capacity then the default capacity will be used instead. The integer value after initializing is 0.

fn initSet(allocator: Allocator, value: anytype) !Managed

Creates a new Managed with value value.

Creates a new Managed with value value.

This is identical to an init, followed by a set.

fn isEven(self: Managed) bool

No documentation provided.

fn isOdd(self: Managed) bool

No documentation provided.

fn isPositive(self: Managed) bool

Returns whether an Managed is positive.

fn len(self: Managed) usize

Returns the number of limbs currently in use.

fn mul(rma: *Managed, a: *const Managed, b: *const Managed) !void

rma = a * b

rma = a * b

rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.

Returns an error if memory could not be allocated.

rma’s allocator is used for temporary storage to speed up the multiplication.

fn mulWrap(rma: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) !void

rma = a * b with 2s-complement wrapping semantics.

rma = a * b with 2s-complement wrapping semantics.

rma, a and b may be aliases. However, it is more efficient if rma does not alias a or b.

Returns an error if memory could not be allocated.

rma’s allocator is used for temporary storage to speed up the multiplication.

fn negate(self: *Managed) void

Negate the sign.

fn normalize(r: *Managed, length: usize) void

Normalize a possible sequence of leading zeros.

Normalize a possible sequence of leading zeros.

[1, 2, 3, 4, 0] -> [1, 2, 3, 4] [1, 2, 0, 0, 0] -> [1, 2] [0, 0, 0, 0, 0] -> [0]

fn order(a: Managed, b: Managed) math.Order

Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a

b r…

Returns math.Order.lt, math.Order.eq, math.Order.gt if a < b, a == b or a

b respectively.

fn orderAbs(a: Managed, b: Managed) math.Order

Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| == |b| or…

Returns math.Order.lt, math.Order.eq, math.Order.gt if |a| < |b|, |a| == |b| or |a| > |b| respectively.

fn popCount(r: *Managed, a: *const Managed, bit_count: usize) !void

r = @popCount(a) with 2s-complement semantics. r and a may be aliases.

fn pow(rma: *Managed, a: *const Managed, b: u32) !void

No documentation provided.

fn saturate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void

r = saturate(Int(signedness, bit_count), a)

fn set(self: *Managed, value: anytype) Allocator.Error!void

Sets an Managed to value. Value must be an primitive integer type.

fn setLen(self: *Managed, new_len: usize) void

Sets the length of an Managed.

Sets the length of an Managed.

If setLen is used, then the Managed must be normalized to suit.

fn setMetadata(self: *Managed, positive: bool, length: usize) void

No documentation provided.

fn setSign(self: *Managed, positive: bool) void

Sets the sign of an Managed.

fn setString(self: *Managed, base: u8, value: []const u8) !void

Set self from the string representation value.

Set self from the string representation value.

value must contain only digits <= base and is case insensitive. Base prefixes are not allowed (e.g. 0x43 should simply be 43). Underscores in the input string are ignored and can be used as digit separators.

Returns an error if memory could not be allocated or value has invalid digits for the requested base.

self’s allocator is used for temporary storage to boost multiplication performance.

fn setTwosCompIntLimit(r: *Managed, limit: TwosCompIntLimit, signedness: Signedness, bit_count: usize) !void

Set self to either bound of a 2s-complement integer. Note: The result is still …

Set self to either bound of a 2s-complement integer. Note: The result is still sign-magnitude, not twos complement! In order to convert the result to twos complement, it is sufficient to take the absolute value.

fn shiftLeft(r: *Managed, a: *const Managed, shift: usize) !void

r = a << shift, in other words, r = a * 2^shift r and a may alias.

fn shiftLeftSat(r: *Managed, a: *const Managed, shift: usize, signedness: Signedness, bit_count: usize) !void

r = a <<| shift with 2s-complement saturating semantics. r and a may alias.

fn shiftRight(r: *Managed, a: *const Managed, shift: usize) !void

r = a >> shift r and a may alias.

fn sizeInBaseUpperBound(self: Managed, base: usize) usize

Returns the approximate size of the integer in the given base. Negative values a…

Returns the approximate size of the integer in the given base. Negative values accommodate for the minus sign. This is used for determining the number of characters needed to print the value. It is inexact and may exceed the given value by ~1-2 bytes.

fn sqr(rma: *Managed, a: *const Managed) !void

r = a * a

fn sqrt(rma: *Managed, a: *const Managed) !void

r = ⌊√a⌋

fn sub(r: *Managed, a: *const Managed, b: *const Managed) !void

r = a - b

r = a - b

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn subSat(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!void

r = a - b with 2s-complement saturating semantics.

r = a - b with 2s-complement saturating semantics.

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn subWrap(r: *Managed, a: *const Managed, b: *const Managed, signedness: Signedness, bit_count: usize) Allocator.Error!bool

r = a - b with 2s-complement wrapping semantics. Returns whether any overflow oc…

r = a - b with 2s-complement wrapping semantics. Returns whether any overflow occurred.

r, a and b may be aliases.

Returns an error if memory could not be allocated.

fn swap(self: *Managed, other: *Managed) void

Efficiently swap a Managed with another. This swaps the limb pointers and a fu…

Efficiently swap a Managed with another. This swaps the limb pointers and a full copy is not performed. The address of the limbs field will not be the same after this function.

fn to(self: Managed, comptime T: type) ConvertError!T

Convert self to type T.

Convert self to type T.

Returns an error if self cannot be narrowed into the requested type without truncation.

fn toConst(self: Managed) Const

No documentation provided.

fn toMutable(self: Managed) Mutable

No documentation provided.

fn toString(self: Managed, allocator: Allocator, base: u8, case: std.fmt.Case) ![]u8

Converts self to a string in the requested base. Memory is allocated from the pr…

Converts self to a string in the requested base. Memory is allocated from the provided allocator and not the one present in self.

fn truncate(r: *Managed, a: *const Managed, signedness: Signedness, bit_count: usize) !void

r = truncate(Int(signedness, bit_count), a)

Values

default_capacity
comptime_int

Default number of limbs to allocate on creation of a Managed.

eq
undefined
eqAbs
undefined
eqZero
undefined
sign_bit
usize

Error Sets