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 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 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 cloneWithDifferentAllocator(other: Managed, allocator: Allocator) !Managed
No documentation provided.
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 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 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 withManaged
. If the integer is larger than…To allow
std.fmt.format
to work withManaged
. If the integer is larger thanpow(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
toStringand
toStringAlloc` 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 is0
.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 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 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 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 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 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 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 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 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 |
eq | undefined | |
eqAbs | undefined | |
eqZero | undefined | |
sign_bit | usize |