diff --git a/packages/cultcache-ts/src/codec.ts b/packages/cultcache-ts/src/codec.ts index c7d2736d..1ad50f8e 100644 --- a/packages/cultcache-ts/src/codec.ts +++ b/packages/cultcache-ts/src/codec.ts @@ -88,11 +88,11 @@ export const int32 = integerCodec("System.Int32", "an int32", -(2n ** 31n), 2n * export const int64 = integerCodec("System.Int64", "an int64", -(2n ** 63n), 2n ** 63n - 1n); // A 64-bit integer to the nearest float32 in one rounding, ties to even, as the reference's cast does. Number(value) -// first would round to a double and then again, and the two roundings can disagree. +// first would round to a double and then again, and the two roundings can disagree. A bigint reaches here only beyond +// 2^53 (the wire reader returns a number for any smaller integer), so at least 30 bits are rounded away. function bigintToFloat32(value: bigint): number { const magnitude = value < 0n ? -value : value; const excess = magnitude.toString(2).length - 24; - if (excess <= 0) return Number(value); const kept = magnitude >> BigInt(excess); const rest = magnitude - (kept << BigInt(excess)); const half = 1n << BigInt(excess - 1); diff --git a/packages/cultcache-ts/test/union-parity.test.ts b/packages/cultcache-ts/test/union-parity.test.ts index 69727b8f..6b31c902 100644 --- a/packages/cultcache-ts/test/union-parity.test.ts +++ b/packages/cultcache-ts/test/union-parity.test.ts @@ -548,7 +548,7 @@ const isNaNBits = (bitsHex: string) => { test("numeric leaves read and refuse exactly what the C# reference does", async () => { const rows = readWire(new Uint8Array(await readFile(join(numericVectors, "reads.msgpack")))) as [string, Uint8Array, boolean, Uint8Array][]; - assert.equal(rows.length, 148); + assert.equal(rows.length, 156); let accepted = 0; let refused = 0; for (const [leaf, payload, wasAccepted, bits] of rows) { diff --git a/tests/vectors/numeric-leaves/reads.msgpack b/tests/vectors/numeric-leaves/reads.msgpack index 45aaf7f4..51991ff2 100644 Binary files a/tests/vectors/numeric-leaves/reads.msgpack and b/tests/vectors/numeric-leaves/reads.msgpack differ