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.fp

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

struct Fp(size_t coefficientSize) if (coefficientSize % (size_t.sizeof * 8) == 0 && (coefficientSize >= size_t.sizeof * 8));
Software floating point bumber.
Parameters:
coefficientSize coefficient size in bits
nothrow this(bool sign, sizediff_t exponent, UInt!coefficientSize normalizedCoefficient);
nothrow this(size_t size)(UInt!size integer, bool normalizedInteger = false);
Examples:
auto fp = Fp!128(UInt!128.fromHexString("afbbfae3cd0aff2714a1de7022b0029d"));
assert(fp.exponent == 0);
assert(fp.coefficient == UInt!128.fromHexString("afbbfae3cd0aff2714a1de7022b0029d"));

fp = Fp!128(UInt!128.fromHexString("afbbfae3cd0aff2714a1de7022b0029d"), true);
assert(fp.exponent == 0);
assert(fp.coefficient == UInt!128.fromHexString("afbbfae3cd0aff2714a1de7022b0029d"));

fp = Fp!128(UInt!128.fromHexString("ae3cd0aff2714a1de7022b0029d"));
assert(fp.exponent == -20);
assert(fp.coefficient == UInt!128.fromHexString("ae3cd0aff2714a1de7022b0029d00000"));

fp = Fp!128(UInt!128.fromHexString("e7022b0029d"));
assert(fp.exponent == -84);
assert(fp.coefficient == UInt!128.fromHexString("e7022b0029d000000000000000000000"));

fp = Fp!128(UInt!64.fromHexString("e7022b0029d"));
assert(fp.exponent == -84);
assert(fp.coefficient == UInt!128.fromHexString("e7022b0029d000000000000000000000"));

fp = Fp!128(UInt!64.fromHexString("e7022b0029dd0aff"), true);
assert(fp.exponent == -64);
assert(fp.coefficient == UInt!128.fromHexString("e7022b0029dd0aff0000000000000000"));

fp = Fp!128(UInt!64.fromHexString("e7022b0029d"));
assert(fp.exponent == -84);
assert(fp.coefficient == UInt!128.fromHexString("e7022b0029d000000000000000000000"));

fp = Fp!128(UInt!192.fromHexString("ffffffffffffffffffffffffffffffff1000000000000000"));
assert(fp.exponent == 64);
assert(fp.coefficient == UInt!128.fromHexString("ffffffffffffffffffffffffffffffff"));

fp = Fp!128(UInt!192.fromHexString("ffffffffffffffffffffffffffffffff8000000000000000"));
assert(fp.exponent == 65);
assert(fp.coefficient == UInt!128.fromHexString("80000000000000000000000000000000"));

fp = Fp!128(UInt!192.fromHexString("fffffffffffffffffffffffffffffffe8000000000000000"));
assert(fp.exponent == 64);
assert(fp.coefficient == UInt!128.fromHexString("fffffffffffffffffffffffffffffffe"));

fp = Fp!128(UInt!192.fromHexString("fffffffffffffffffffffffffffffffe8000000000000001"));
assert(fp.exponent == 64);
assert(fp.coefficient == UInt!128.fromHexString("ffffffffffffffffffffffffffffffff"));
nothrow ref Fp opOpAssign(string op : "*")(Fp rhs) return;
const nothrow Fp opBinary(string op : "*")(Fp rhs);
Examples:
auto a = Fp!128(0, -13, UInt!128.fromHexString("dfbbfae3cd0aff2714a1de7022b0029d"));
auto b = Fp!128(1, 100, UInt!128.fromHexString("e3251bacb112c88b71ad3f85a970a314"));
auto fp = a * b;
assert(fp.sign);
assert(fp.exponent == 100 - 13 + 128);
assert(fp.coefficient == UInt!128.fromHexString("c6841dd302415d785373ab6d93712988"));
const nothrow T opCast(T)()
if (is(Unqual!T == bool));
const nothrow T opCast(T, bool noHalf = false)()
if (isFloatingPoint!T);
Examples:
auto fp = Fp!128(1, 100, UInt!128.fromHexString("e3251bacb112cb8b71ad3f85a970a314"));
assert(cast(double)fp == -0xE3251BACB112C8p+172);
Examples:
auto fp = Fp!128(1, 100, UInt!128.fromHexString("e3251bacb112cb8b71ad3f85a970a314"));
static if (real.mant_dig == 64)
    assert(cast(real)fp == -0xe3251bacb112cb8bp+164L);
Examples:
auto fp = Fp!64(1, 100, UInt!64(0xe3251bacb112cb8b));
version (DigitalMars)
{
    // https://issues.dlang.org/show_bug.cgi?id=20963
    assert(cast(double)fp == -0xE3251BACB112C8p+108
        || cast(double)fp == -0xE3251BACB112D0p+108);
}
else
{
    assert(cast(double)fp == -0xE3251BACB112C8p+108);
}
Examples:
auto fp = Fp!64(1, 100, UInt!64(0xe3251bacb112cb8b));
static if (real.mant_dig == 64)
    assert(cast(real)fp == -0xe3251bacb112cb8bp+100L);
const nothrow T opCast(T : Fp!newCoefficientSize, size_t newCoefficientSize)();
Examples:
auto fp = cast(Fp!64) Fp!128(UInt!128.fromHexString("afbbfae3cd0aff2784a1de7022b0029d"));
assert(fp.exponent == 64);
assert(fp.coefficient == UInt!64.fromHexString("afbbfae3cd0aff28"));
pure nothrow @nogc @safe Fp!(coefficientizeA + coefficientizeB) extendedMul(size_t coefficientizeA, size_t coefficientizeB)(Fp!coefficientizeA a, Fp!coefficientizeB b);