A arbitrary-precision big integer, with a fixed set of immutable limbs.

Fields

limbs: []const Limb,

Raw digits. These are:

  • Little-endian ordered
  • limbs.len >= 1
  • Zero is represented as limbs.len == 1 with limbs[0] == 0.

Accessing limbs directly should be avoided.

positive: bool,

Functions

fn abs(self: Const) Const

No documentation provided.

fn bitCountAbs(self: Const) 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: Const) 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 clz(a: Const, bits: Limb) Limb

No documentation provided.

fn ctz(a: Const, bits: Limb) Limb

No documentation provided.

fn dump(self: Const) void

No documentation provided.

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

Returns true if a == b.

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

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

fn eqlZero(a: Const) bool

Returns true if a == 0.

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

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

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

No documentation provided.

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

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

To allow std.fmt.format to work with this type. 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 isEven(self: Const) bool

No documentation provided.

fn isOdd(self: Const) bool

No documentation provided.

fn negate(self: Const) Const

No documentation provided.

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

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

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

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

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

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

fn orderAgainstScalar(lhs: Const, scalar: anytype) math.Order

Same as order but the right-hand operand is a primitive integer.

fn popCount(self: Const, bit_count: usize) usize

@popCount with two’s complement semantics.

@popCount with two’s complement semantics.

This returns the number of 1 bits set when the value would be represented in two’s complement with the given integer width (bit_count). This includes the leading sign bit, which will be set for negative values.

Asserts that bit_count is enough to represent value in two’s compliment and that the final result fits in a usize. Asserts that there are no trailing empty limbs on the most significant end, i.e. that limb count matches calcLimbLen() and zero is not negative.

fn sizeInBaseUpperBound(self: Const, 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. TODO See if we can make this exact.

fn to(self: Const, 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 toManaged(self: Const, allocator: Allocator) Allocator.Error!Managed

The result is an independent resource which is managed by the caller.

fn toMutable(self: Const, limbs: []Limb) Mutable

Asserts limbs is big enough to store the value.

fn toString(self: Const, string: []u8, base: u8, case: std.fmt.Case, limbs_buffer: []Limb) usize

Converts self to a string in the requested base. Asserts that base is in the …

Converts self to a string in the requested base. Asserts that base is in the range [2, 16]. string is a caller-provided slice of at least sizeInBaseUpperBound bytes, where the result is written to. Returns the length of the string. limbs_buffer is caller-provided memory for toString to use as a working area. It must have length of at least calcToStringLimbsBufferLen. In the case of power-of-two base, limbs_buffer is ignored. See also toStringAlloc, a higher level function than this.

fn toStringAlloc(self: Const, allocator: Allocator, base: u8, case: std.fmt.Case) Allocator.Error![]u8

Converts self to a string in the requested base. Caller owns returned memory. …

Converts self to a string in the requested base. Caller owns returned memory. Asserts that base is in the range [2, 16]. See also toString, a lower level function than this.

fn writePackedTwosComplement(x: Const, buffer: []u8, bit_offset: usize, bit_count: usize, endian: Endian) void

Write the value of x to a packed memory buffer. Asserts that buffer is la…

Write the value of x to a packed memory buffer. Asserts that buffer is large enough to contain a value of bit-size bit_count at offset bit_offset.

This is equivalent to storing the value of an integer with bit_count bits as if it were a field in packed memory at the provided bit offset.

fn writeTwosComplement(x: Const, buffer: []u8, endian: Endian) void

Write the value of x into buffer Asserts that buffer is large enough to s…

Write the value of x into buffer Asserts that buffer is large enough to store the value.

buffer is filled so that its contents match what would be observed via @ptrCast(*[buffer.len]const u8, &x). Byte ordering is determined by endian, and any required padding bits are added on the MSB end.

Values

eq
undefined
eqAbs
undefined
eqZero
undefined

Error Sets