Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

mir.bignum.integer

Note The module doesn't provide full arithmetic API for now.

struct BigInt(size_t maxSize64) if (maxSize64 && (maxSize64 <= (ushort).max));
Stack-allocated big signed integer.
Parameters:
maxSize64 count of 64bit words in coefficient
Examples:
import mir.bignum.fixed;
import mir.bignum.low_level_view;

auto a = BigInt!4.fromHexString("4b313b23aa560e1b0985f89cbe6df5460860e39a64ba92b4abdd3ee77e4e05b8");
auto b = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
auto c = BigInt!4.fromHexString("7869dd864619cace5953a09910327b3971413e6aa5f417fa25a2ac93291b941f");
c.sign = true;
assert(a != b);
assert(a < b);
a -= b;
assert(a.sign);
assert(a == c);
a -= a;
assert(!a.sign);
assert(!a.length);

auto d = BigInt!4.fromHexString("0de1a911c6dc8f90a7169a148e65d22cf34f6a8254ae26362b064f26ac44218a");
assert((b *= 0x7869dd86) == 0x5c019770);
assert(b == d);

d = BigInt!4.fromHexString("856eeb23e68cc73f2a517448862cdc97e83f9dfa23768296724bf00fda7df32a");
auto o = b *= UInt!128.fromHexString("f79a222050aaeaaa417fa25a2ac93291");
assert(o == UInt!128.fromHexString("d6d15b99499b73e68c3331eb0f7bf16"));
assert(b == d);

d = BigInt!4.fromHexString("d"); // initial value
d.mulPow5(60);
c = BigInt!4.fromHexString("81704fcef32d3bd8117effd5c4389285b05d");
assert(d == c);

d >>= 80;
c = BigInt!4.fromHexString("81704fcef32d3bd8");
assert(d == c);

c = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
d = BigInt!4.fromHexString("9935cea0707f79a222050aaeaaaed17feb7aa76999d700000000000000000000");
c <<= 80;
assert(d == c);
c >>= 80;
c <<= 84;
d <<= 4;
assert(d == c);
assert(c != b);
b.sign = true;
assert(!c.copyFrom(b.view));
assert(c == b);
b >>= 18;
auto bView = cast(BigIntView!ushort)b.view;
assert(!c.copyFrom(bView.topLeastSignificantPart(bView.unsigned.coefficients.length - 1)));
assert(c == b);
bool sign;
uint length;
size_t[(ulong).sizeof / size_t.sizeof * maxSize64] data;
this(size_t size)(UInt!size fixedInt);
this(size_t N)(size_t[N] data)
if (N <= this.data.length);
this(ulong data);
pure @nogc @safe this()(scope const(char)[] str);
ref auto opAssign(ulong data) return;
Examples:
import mir.math.constant: PI;
BigInt!4 integer = "-34010447314490204552169750449563978034784726557588085989975288830070948234680"; // constructor
assert(integer.sign);
integer.sign = false;
assert(integer == BigInt!4.fromHexString("4b313b23aa560e1b0985f89cbe6df5460860e39a64ba92b4abdd3ee77e4e05b8"));
ref auto opAssign(size_t rhsMaxSize64)(auto ref scope const BigInt!rhsMaxSize64 rhs) return
if (rhsMaxSize64 < maxSize64);
pure nothrow @nogc @safe bool fromStringImpl(C)(scope const(C)[] str)
if (isSomeChar!C);
Returns:
false in case of overflow or incorrect string.

Precondition non-empty coefficients.

@property BigInt copy();
const pure nothrow @nogc @safe bool opEquals()(auto ref const BigInt rhs);
const pure nothrow @nogc @safe bool opEquals()(size_t rhs, bool rhsSign = false);
const pure nothrow @nogc @safe bool opEquals()(sizediff_t rhs);
const pure nothrow @nogc @safe auto opCmp()(auto ref const BigInt rhs);
@property BigIntView!size_t view()();
const @property BigIntView!(const(size_t)) view()();
void normalize()();
void putCoefficient(size_t value);
pure nothrow @nogc @safe size_t opOpAssign(string op : "*")(size_t rhs, size_t overflow = 0u);
Performs size_t overflow = (big += overflow) *= scalar operatrion.
Parameters:
size_t rhs unsigned value to multiply by
size_t overflow initial overflow value
Returns:
unsigned overflow value
pure nothrow @nogc @safe uint opOpAssign(string op : "/")(uint rhs, uint overflow = 0);
Performs uint remainder = (overflow$big) /= scalar operatrion, where $ denotes big-endian concatenation.

Precondition overflow < rhs

Parameters:
uint rhs unsigned value to devide by
uint overflow initial unsigned overflow
Returns:
unsigned remainder value (evaluated overflow)
pure nothrow @nogc @safe UInt!size opOpAssign(string op : "*", size_t size)(UInt!size rhs, UInt!size overflow = 0);
Performs size_t overflow = (big += overflow) *= fixed operatrion.
Parameters:
UInt!size rhs unsigned value to multiply by
UInt!size overflow initial overflow value
Returns:
unsigned overflow value
pure nothrow @nogc @safe bool opOpAssign(string op, size_t rhsMaxSize64)(ref const BigInt!rhsMaxSize64 rhs)
if (op == "+" || op == "-");

pure nothrow @nogc @safe bool opOpAssign(string op)(BigIntView!(const(size_t)) rhs)
if (op == "+" || op == "-");
Performs size_t overflow = big *= fixed operatrion.
Parameters:
BigInt!rhsMaxSize64 rhs unsigned value to multiply by
Returns:
overflow
pure @trusted BigInt fromHexString()(scope const(char)[] str);
pure nothrow @nogc @safe bool fromHexStringImpl(C)(scope const(C)[] str)
if (isSomeChar!C);
bool mulPow5(size_t degree);
pure nothrow @nogc ref @safe BigInt opOpAssign(string op)(size_t shift) return
if (op == "<<" || op == ">>");
const T opCast(T, bool wordNormalized = false, bool nonZero = false)()
if (isFloatingPoint!T && isMutable!T);
const T opCast(T, bool nonZero = false)()
if (is(T == long) || is(T == int));
bool copyFrom(W, WordEndian endian)(BigIntView!(const(W), endian) view);

bool copyFrom(W, WordEndian endian)(BigUIntView!(const(W), endian) view);
Returns:
overflow flag
const pure nothrow @safe immutable(C)[] toString(C = char)()
if (isSomeChar!C && isMutable!C);
Examples:
auto str = "-34010447314490204552169750449563978034784726557588085989975288830070948234680";
auto integer = BigInt!4(str);
assert(integer.toString == str);

integer = BigInt!4.init;
assert(integer.toString == "0");
const void toString(C = char, W)(ref scope W w)
if (isSomeChar!C && isMutable!C);
Examples:
Check @nogc toString impl
import mir.format: stringBuf;
auto str = "-34010447314490204552169750449563978034784726557588085989975288830070948234680";
auto integer = BigInt!4(str);
stringBuf buffer;
buffer << integer;
assert(buffer.data == str, buffer.data);