Fp.this

Constructs Fp from hardaware floating point number.

  1. this(bool sign, Exp exponent, UInt!coefficientSize normalizedCoefficient)
  2. this(T value, bool normalize)
    struct Fp(size_t coefficientSize, Exp = sizediff_t)
    @safe pure nothrow @nogc
    this
    (
    T
    )
    (
    const T value
    ,
    bool normalize = true
    )
    if (
    isFloatingPoint!T &&
    T.mant_dig <= coefficientSize
    )
    if (
    (
    is(Exp == int) ||
    is(Exp == long)
    )
    &&
    coefficientSize % (size_t.sizeof * 8) == 0
    &&
    coefficientSize >= (size_t.sizeof * 8)
    )
  3. this(UInt!size integer, bool normalizedInteger)

Parameters

value T

Hardware floating point number. Special values nan and inf aren't allowed.

normalize bool

flag to indicate if the normalization should be performed.

Examples

enum h = -33.0 * 2.0 ^^ -10;
auto f = Fp!64(h);
assert(f.sign);
assert(f.exponent == -10 - (64 - 6));
assert(f.coefficient == 33UL << (64 - 6));
assert(cast(double) f == h);

// CTFE
static assert(cast(double) Fp!64(h) == h);

f = Fp!64(-0.0);
assert(f.sign);
assert(f.exponent == 0);
assert(f.coefficient == 0);

// subnormals
static assert(cast(float) Fp!64(float.min_normal / 2) == float.min_normal / 2);
static assert(cast(float) Fp!64(float.min_normal * float.epsilon) == float.min_normal * float.epsilon);
// subnormals
static assert(cast(double) Fp!64(double.min_normal / 2) == double.min_normal / 2);
static assert(cast(double) Fp!64(double.min_normal * double.epsilon) == double.min_normal * double.epsilon);
// subnormals
static assert(cast(real) Fp!64(real.min_normal / 2) == real.min_normal / 2);
static assert(cast(real) Fp!64(real.min_normal * real.epsilon) == real.min_normal * real.epsilon);

enum d = cast(float) Fp!64(float.min_normal / 2, false);

// subnormals
static assert(cast(float) Fp!64(float.min_normal / 2, false) == float.min_normal / 2, d.stringof);
static assert(cast(float) Fp!64(float.min_normal * float.epsilon, false) == float.min_normal * float.epsilon);
// subnormals
static assert(cast(double) Fp!64(double.min_normal / 2, false) == double.min_normal / 2);
static assert(cast(double) Fp!64(double.min_normal * double.epsilon, false) == double.min_normal * double.epsilon);
// subnormals
static assert(cast(real) Fp!64(real.min_normal / 2, false) == real.min_normal / 2);
static assert(cast(real) Fp!64(real.min_normal * real.epsilon, false) == real.min_normal * real.epsilon);

Without normalization

auto f = Fp!64(-33.0 * 2.0 ^^ -10, false);
assert(f.sign);
assert(f.exponent == -10 - (double.mant_dig - 6));
assert(f.coefficient == 33UL << (double.mant_dig - 6));

Meta