isIntegralBijectableType

Undocumented in source.
enum isIntegralBijectableType (
T
)

Examples

check that bijectToUnsigned preserves orderness, that is is a bijection

import std.random : Random, uniform;
auto gen = Random();

enum maxCount = 1e4;
import std.algorithm : min;

static int cmp(T)(T x, T y) => x < y ? -1 : x > y ? 1 : 0;

foreach (T; AliasSeq!(ubyte, ushort, uint, ulong, byte, short, int, long)) {
	foreach (i; 0 .. min(maxCount, T.max - T.min)) {
		const x = uniform(T.min, T.max, gen);
		const y = uniform(T.min, T.max, gen);

		const expected = cmp(x, y);
		const result = cmp(x.bijectToUnsigned, y.bijectToUnsigned);

		assert(result == expected);
	}
}

foreach (T; AliasSeq!(float, double)) {
	foreach (i; 0 .. maxCount) {
		const T x = uniform(-1e20, +1e20, gen);
		const T y = uniform(-1e20, +1e20, gen);

		// import nxt.debugio;
		// dbg(x, ",", y);

		const expected = cmp(x, y);
		const result = cmp(x.bijectToUnsigned, y.bijectToUnsigned);

		assert(result == expected);
	}
}

Meta