ddn.num.fixed_binary
ddn.num.fixed_binary — binary fixed-point values.
FixedBinary!(Storage, FractionBits) represents values as raw × 2⁻ᶠʳᵃᶜᵗⁱᵒⁿᴮⁱᵗˢ over a signed integer storage — the binary sibling of fixed_decimal (coefficient × 10⁻ˢᶜᵃˡᵉ). Fixed point is one concept with two radices; a reader of one family knows the other. The decimal family is exact and throws on inexact operations; the binary family mirrors the C libraries and wire formats it serves:
- Rounding is the normal operation. Operations that reduce the
scale (multiplication, division,
atScaleto fewer bits, string/floating conversion) round under an explicit RoundingMode;NEAREST_EVENis the default. - Overflow of the raw value wraps like the integer it wraps
(addition, subtraction, unary minus,
abs, the final reduction ofmul, integer `*`/`+`/`-`). Checked operations —fromString,fromDouble,div,divInt,atScale— returnResult!(FixedBinary, FixedBinaryError)instead. - The core arithmetic subset is
@safe pure nothrow @nogcandCTFE-able. Allocating conveniences (
toString,toDecimal, toBinary) are pure but not@nogc. abs(I.min)wraps toI.min;divIntof the raw minimum by-1yields the wrapping quotientI.min; both are explicitly tested.- Multiplication and division compute through a proven
double-width intermediate (
longforintstorage,Int128forlongstorage), so the intermediate never overflows before rescaling.
Domain aliases:
- Fixed26_6 — pixel positions and advances of the text
roles (
ddn.cti.text), the FreeType/HarfBuzz 26.6 convention. - Fixed24_8 — Wayland's
wl_fixed_t. Conversion roundingis explicit: the checked-in Wayland 1.26.0 vectors require
HALF_AWAY, while this numeric family's default remainsNEAREST_EVEN. Do not assume all Wayland versions have the same tie rule; seeit/fixed_binary/README.md. - Fixed16_16 — X11 RENDER
FIXED, XInput 2FP1616,FreeType
FT_Fixed, OpenType variation coordinates.
The type is standalone (Phobos plus ddn.result/ddn.num.fixed_decimal only) and every non-allocating operation is CTFE-safe.
Design record: doc/DDN-FIXED-BINARY.md.
Example:
import ddn.num.fixed_binary;
enum one = Fixed26_6.fromInt(1);
static assert(one.unscaled == 64);
const third = Fixed24_8.fromDouble(1.0 / 3).value();
assert(third.unscaled == 85); // 85.333… rounds to 85
assert(Fixed26_6.fromString("12.5").value().toString() == "12.5");
const back = one.atScale!8().value(); // 26.6 → 24.8
assert(back.unscaled == 256);Types 7
Failure codes of the checked FixedBinary operations.
The recoverable error of ddn.num.fixed_binary.
FixedBinaryErrorCode codeWhich failure happened.Rounding applied when an operation discards fraction bits.
Multiplication, division, atScale reductions, round/toInt and the text/floating conversions accept an explicit mode. Where a mode has a default it is NEAREST_EVEN; toInt() and divInt() without a mode are the documented truncating integer conveniences.
Binary fixed-point value: unscaled() × 2⁻ˢᶜᵃˡᵉ.
Parameters
I | signed storage: short, int or long. |
FractionBits | number of fraction bits; 0 < FractionBits < I.sizeof * 8 - 1. |
int scaleThe scale: number of fraction bits. The value represented is `unscaled() × 2⁻ˢᶜᵃˡᵉ`. (In `FixedDecimal` the same word counts decimal digits; here it counts binary digits.)FixedBinary fromUnscaled(I raw) static @safe pure nothrow @nogcWraps a raw (unscaled) integer: the value the C libraries and the wire carry. No scaling, no checking.I unscaled() const @safe pure nothrow @nogcThe raw (unscaled) coefficient: what the C libraries and the wire carry.FixedBinary fromInt(I value) static @safe pure nothrow @nogcBuilds an integer value. Overflows of the integer range wrap like the storage integer (checked conversions are `fromString`, `fromDouble` and `atScale`).I toInt(RoundingMode mode) const @safe pure nothrow @nogcInteger conversion under an explicit rounding mode.FixedBinary ceil() const @safe pure nothrow @nogcRounds toward +infinity to an integer value. Wraps when the ceiling of the maximum value is computed.FixedBinary round(RoundingMode mode) const @safe pure nothrow @nogcRounds to an integer value under an explicit rounding mode. The result wraps when the rounded integer does not fit the integer bits of the storage.FixedBinary abs() const @safe pure nothrow @nogcAbsolute value. `abs(I.min)` wraps to `I.min`, exactly like the storage integer (explicitly tested).Result!(FixedBinary, FixedBinaryError) fromDouble(double value,
RoundingMode mode = RoundingMode.NEAREST_EVEN) static @safe pure nothrow @nogcConverts a finite `double`, rounded to the nearest representable value per `mode` (default `NEAREST_EVEN`; use `HALF_AWAY` to match the tested Wayland 1.26.0 conversion). The scaling is exact, so t...double toDouble() const @safe pure nothrow @nogcConverts to `double`. Values needing more than 53 significant bits round to nearest (IEEE 754 default), as with any integer-to-double conversion.Result!(FixedBinary, FixedBinaryError) fromString(scope const(char)[] text,
RoundingMode mode = RoundingMode.NEAREST_EVEN) static @safe pure nothrow @nogcParses decimal text, rounding to the nearest representable value per `mode`. Grammar: optional `+`/`-`, digits with at most one `.`, at least one digit overall; no exponents, no grouping, ASCII dig...size_t formatTo(scope char[] outBuf) const @safe pure nothrow @nogcFormats the exact decimal text into a caller-provided buffer (capacity protocol). The integer part is printed in full; the fraction is exact — n fraction bits print as at most n decimal digits, t...string toString() const @safe pure nothrowThe exact decimal text, allocated (the `@nogc`-free convenience form of formatTo).FixedBinary opBinary(string op)(FixedBinary rhs) if (op == "+" || op == "-") constFixed-point addition/subtraction. Overflows wrap like the storage integer (explicit unsigned arithmetic, as in C fixed-point code).FixedBinary opBinary(string op)(I rhs) if (op == "+" || op == "-" || op == "*") constInteger addition/subtraction at the current scale, and integer multiplication. Overflows wrap like the storage integer.FixedBinary opBinaryRight(string op)(I lhs) if (op == "*" || op == "+" || op == "-") constInteger-left arithmetic, with the same wrapping policy. Params: lhs = integer operand. Returns: the sum, difference or integer product.FixedBinary opBinary(string op)(FixedBinary rhs) if (op == "*") const @safe pure nothrow @nogcFixed-point multiplication with the default rounding policy. Params: rhs = fixed-point factor. Returns: the product, wrapping after scale reduction.FixedBinary opUnary(string op)() if (op == "-") constNegation. Wraps at the raw minimum, like the storage integer.bool opEquals(FixedBinary rhs) const @safe pure nothrow @nogcEquality and three-way comparison by value.bool opEquals(I rhs) const @safe pure nothrow @nogcCompares exactly with an integer, without wrapping or narrowing it into the fixed-point range. Params: rhs = integer to compare. Returns: true when the values are equal.int opCmp(I rhs) const @safe pure nothrow @nogcExact fixed-point/integer ordering. Params: rhs = integer to compare. Returns: negative, zero or positive as this value is below, equal to or above the integer.FixedBinary mul(FixedBinary rhs, RoundingMode mode = RoundingMode.NEAREST_EVEN) const @safe pure nothrow @nogcFixed-point multiplication: `this × rhs`, computed in a proven double-width intermediate (the product is exact before reduction) and rounded back to `scale` bits per `mode`. The reduced result wra...Result!(FixedBinary, FixedBinaryError) div(FixedBinary rhs,
RoundingMode mode = RoundingMode.NEAREST_EVEN) const @safe pure nothrow @nogcChecked fixed-point division: `this ÷ rhs`, computed in a proven double-width intermediate and rounded per `mode`.Result!(FixedBinary, FixedBinaryError) divInt(I rhs) const @safe pure nothrow @nogcChecked division by an integer (truncating toward zero, like C integer division). The one overflowing quotient, raw `I.min ÷ -1`, wraps to `I.min` by this API's explicit policy, without executing ...Result!(FixedBinary!(I, Bits), FixedBinaryError) atScale(int Bits)(
RoundingMode mode = RoundingMode.NEAREST_EVEN) const @safe pure nothrow @nogcRe-scales to a different fraction-bit count over the same storage. Growing is exact and checked; reducing rounds per `mode`.26.6 fixed point over int: pixel positions and advances of the ddn.cti.text roles; the FreeType/HarfBuzz convention.
24.8 fixed point over int: Wayland's wl_fixed_t.
16.16 fixed point over int: X11 RENDER FIXED, XInput 2 FP1616, FreeType FT_Fixed, OpenType variation coordinates.
Functions 2
FixedDecimal!S toDecimal(S, I, int Bits)(FixedBinary!(I, Bits) value) @safe pureConverts exactly from binary to decimal fixed point. This is a free function (also callable with UFCS); neither family needs to depend on conversion members in the other. The decimal family's own p...Result!(FixedBinary!(I, Bits), FixedBinaryError) toBinary(I, int Bits, S, int Sc)(
FixedDecimal!(S, Sc) value, RoundingMode mode = RoundingMode.NEAREST_EVEN) @safe pureConverts a decimal fixed-point value to the binary family, rounding per `mode` (exact rational conversion: the decimal text is exact, the binary parse rounds at the target quantum). Not `@nogc` (st...Templates 1
Double-width intermediate of a storage type (Int128 for long).