The OpenD Programming Language

DoubleRep

Allows manipulating the fraction, exponent, and sign parts of a double separately. The definition is:

struct DoubleRep
{
    union
    {
        double value;
        mixin(bitfields!(
                  ulong,   "fraction", 52,
                  ushort,  "exponent", 11,
                  bool,    "sign",      1));
    }
    enum uint bias = 1023, signBits = 1, fractionBits = 52, exponentBits = 11;
}
import std.bitmanip;
alias DoubleRep = FloatingPointRepresentation!double

Examples

DoubleRep rep = {value: 0};
assert(rep.fraction == 0);
assert(rep.exponent == 0);
assert(!rep.sign);

rep.value = 42;
assert(rep.fraction == 1407374883553280);
assert(rep.exponent == 1028);
assert(!rep.sign);

rep.value = 10;
assert(rep.fraction == 1125899906842624);
assert(rep.exponent == 1026);
DoubleRep rep = {value: 1};
assert(rep.fraction == 0);
assert(rep.exponent == 1023);
assert(!rep.sign);

rep.exponent = 1022;
assert(rep.value == 0.5);

rep.exponent = 1026;
assert(rep.value == 8);
DoubleRep rep = {value: 1};
rep.value = -0.5;
assert(rep.fraction == 0);
assert(rep.exponent == 1022);
assert(rep.sign);

rep.value = -1. / 3;
assert(rep.fraction == 1501199875790165);
assert(rep.exponent == 1021);
assert(rep.sign);

Reading

DoubleRep x;
x.value = 1.0;
assert(x.fraction == 0 && x.exponent == 1023 && !x.sign);
x.value = -0.5;
assert(x.fraction == 0 && x.exponent == 1022 && x.sign);
x.value = 0.5;
assert(x.fraction == 0 && x.exponent == 1022 && !x.sign);

Writing

DoubleRep x;
x.fraction = 1125899906842624;
x.exponent = 1025;
x.sign = true;
assert(x.value == -5.0);

Meta