ts-data-forge
    Preparing search index...
    Uint: {
        add: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt;
        div: (
            x: WithSmallInt,
            y: ToNonZeroIntWithSmallInt<NonNegativeInt>,
        ) => NonNegativeInt;
        fromNumber: (x: number) => NonNegativeInt;
        is: (a: number) => a is NonNegativeInt;
        max: (...values: readonly WithSmallInt<NonNegativeInt>[]) => NonNegativeInt;
        min: (...values: readonly WithSmallInt<NonNegativeInt>[]) => NonNegativeInt;
        MIN_VALUE: 0;
        mul: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt;
        pow: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt;
        random: (
            min?: WithSmallInt<NonNegativeInt>,
            max?: WithSmallInt<NonNegativeInt>,
        ) => NonNegativeInt;
        sub: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt;
    }

    Namespace providing type-safe operations for the Uint branded type.

    The Uint type represents a non-negative integer. Division (div) uses floor division.

    Unlike SafeInt, Uint allows values outside the safe integer range (±2^53 − 1), so very large magnitudes may lose precision in JavaScript's number type.

    Type Declaration

    • add: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt

      Adds two non-negative integers, returning a + b as an Uint.

      const sum = Uint.add(asUint(5), asUint(8));

      assert.isTrue(sum === 13);
    • div: (x: WithSmallInt, y: ToNonZeroIntWithSmallInt<NonNegativeInt>) => NonNegativeInt

      Divides two non-negative integers using floor division (⌊a / b⌋): the result is a / b rounded toward negative infinity, as an Uint.

      const quotient = Uint.div(asUint(10), asUint(4));

      assert.isTrue(quotient === 2);
    • fromNumber: (x: number) => NonNegativeInt

      Converts an arbitrary number into an Uint, rounding to the nearest integer and saturating the result into the range [MIN_VALUE, MAX_VALUE].

      Unlike asUint, this is total: out-of-range inputs are clamped to the nearest representable Uint instead of throwing.

      const fromNegative = Uint.fromNumber(-5);

      const fromPositive = Uint.fromNumber(42);

      assert.isTrue(fromNegative === 0);

      assert.isTrue(fromPositive === 42);
    • is: (a: number) => a is NonNegativeInt

      Type guard that checks if a value is a non-negative integer.

      assert.isTrue(isUint(4));

      assert.isFalse(isUint(-1));

      assert.isTrue(Uint.is(0));

      isUint for usage examples

    • Readonlymax: (...values: readonly WithSmallInt<NonNegativeInt>[]) => NonNegativeInt

      Returns the largest of the given non-negative integers.

      const largest = Uint.max(asUint(7), asUint(3));

      assert.isTrue(largest === 7);
    • Readonlymin: (...values: readonly WithSmallInt<NonNegativeInt>[]) => NonNegativeInt

      Returns the smallest of the given non-negative integers.

      const smallest = Uint.min(asUint(7), asUint(3));

      assert.isTrue(smallest === 3);
    • MIN_VALUE: 0

      The smallest value representable as Uint (the lower saturation target of fromNumber).

    • mul: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt

      Multiplies two non-negative integers, returning a * b as an Uint.

      const product = Uint.mul(asUint(7), asUint(6));

      assert.isTrue(product === 42);
    • pow: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt

      Raises a to the power b, returning a ** b as an Uint (floored to an integer).

      const base = asUint(2);

      const exponent = asUint(5);

      const power = Uint.pow(base, exponent);

      assert.isTrue(power === 32);
    • random: (
          min?: WithSmallInt<NonNegativeInt>,
          max?: WithSmallInt<NonNegativeInt>,
      ) => NonNegativeInt

      Generates a random Uint within the given range.

      The range is inclusive on both ends.

      const min = asUint(0);

      const max = asUint(3);

      const randomValue = Uint.random(min, max);

      assert.isTrue(Uint.is(randomValue));

      assert.isTrue(randomValue >= 0 && randomValue <= 3);
    • sub: (x: WithSmallInt, y: WithSmallInt) => NonNegativeInt

      Subtracts two non-negative integers, returning a - b as an Uint.

      const difference = Uint.sub(asUint(5), asUint(8));

      assert.isTrue(difference === 0);