The OpenD Programming Language

NormalizedInt

Normalized integers express a fractional range of values within an integer data type.

Unsigned integers map the values [0 to the fractional values [0.0 in equal increments.
Signed integers represent the values [-I.max to fractional values [-1.0 in equal increments. I.min is outside the nominal integer range and is clamped to represent -1.0.

Constructors

this
this(I value)

Construct a NormalizedInt from an integer representation.

this
this(F value)

Construct a NormalizedInt from a floating point representation. The value is clamped to the range [min.

Members

Aliases

IntType
alias IntType = I

Integral storage type.

Functions

opBinary
NormalizedInt!I opBinary(NormalizedInt!I rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(NormalizedInt!I rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(NormalizedInt!I rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(T rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(T rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(F rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(NormalizedInt!I rh)

Binary operators.

opBinary
NormalizedInt!I opBinary(int rh)

Binary operators.

opCast
F opCast()

Floating point cast operator.

opCast
NormInt opCast()

Cast between NormalizedInt types.

opCmp
int opCmp(NormalizedInt!I rh)

Comparison operators.

opCmp
int opCmp(T rh)

Comparison operators.

opCmp
int opCmp(F rh)

Comparison operators.

opEquals
bool opEquals(NormalizedInt!I rh)

Equality operators.

opEquals
bool opEquals(T rh)

Integral equality operator.
If rh is outside of the integral range, rh will not be clamped and comparison will return false.

opEquals
bool opEquals(F rh)

Floating point equality operator.
If rh is outside of the nominal range, rh will not be clamped and comparison will return false.

opOpAssign
NormalizedInt!I opOpAssign(T rh)

Binary assignment operators.

opUnary
NormalizedInt!I opUnary()

Unary operators.

Manifest constants

max_float
enum max_float;

Maximum floating point value.

min_float
enum min_float;

Minimum floating point value.

Variables

max
enum I max;

Maximum integral value.

min
enum I min;

Minimum integral value.

value
I value;

The actual value

Examples

auto x = NormalizedInt!ubyte(200);
auto y = NormalizedInt!ubyte(50);

auto z = x + y;                      assert(z == 250);                 // add as expected
z += y;                              assert(z == 255);                 // overflow saturates
                                     assert(cast(float)z == 1.0);      // maximum value is floating point 1.0
z -= x;                              assert(z == 55);                  // subtract as expected
z -= x;                              assert(z == 0);                   // underflow saturates
z = y * 2;                           assert(z == 100);                 // multiply by integer
z = x * 0.5;                         assert(z == 100);                 // multiply by float
z *= 3;                              assert(z == 255);                 // multiply overflow saturates
z *= y;                              assert(z == 50);                  // multiply is performed in normalized space
z *= y;                              assert(z == 9);                   // multiplication rounds *down*
z /= 2;                              assert(z == 4);                   // division works as expected, rounds down
z /= y;                              assert(z == 20);                  // division is performed in normalized space
z /= y * y;                          assert(z == 255);                 // division overflow saturates
z = -z;                              assert(z == 0);                   // unsigned negation saturates at zero

auto u = NormalizedInt!short(-1.0);
auto v = NormalizedInt!short(0.5);

auto w = cast(NormalizedInt!short)x; assert(w == 25700);               // casting to higher precision extends bit-pattern to preserve uniformity
w = -w;                              assert(w == -25700);              // negation works as expected
w *= 2;                              assert(w == -32767 && w == -1.0); // overflow saturates
w *= -0.5;                           assert(w == 16384 && w > 0.5);    // 0.5 is not exactly representable (odd number of positive integers)
w = w^^0.0;                          assert(w == 1.0);                 // pow as expected

// check floating poing comparisons
static assert(NormalizedInt!ubyte(0xFF) == 1.0);
static assert(NormalizedInt!ubyte(0x00) == 0.0);
static assert(NormalizedInt!ubyte(0x80) > 0.5);
static assert(NormalizedInt!ubyte(0x7F) < 0.5);

static assert(NormalizedInt!byte(127) == 1.0);
static assert(NormalizedInt!byte(-127) == -1.0);
static assert(NormalizedInt!byte(-128) == -1.0);
static assert(NormalizedInt!byte(0x00) == 0.0);
static assert(NormalizedInt!byte(0x40) > 0.5);
static assert(NormalizedInt!byte(0x3F) < 0.5);

Meta