Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(laya-ts): spell floats >= 1e16 like CPython in serializeState

thread3d76385206060f
diff --git a/laya-ts/src/common.ts b/laya-ts/src/common.ts
index 04879d57..d99553ea 100644
--- a/laya-ts/src/common.ts
+++ b/laya-ts/src/common.ts
@@ -10,13 +10,17 @@ export interface InternalQ {
optionOrder?: number[];
}
/** Python `json.dumps` for a finite number. JavaScript prints the same shortest round-trip
-digits, but Python switches to exponent notation below 1e-4 (JavaScript: below 1e-6) and pads the
-exponent to two digits (`5e-05`, not `5e-5`). An integer-valued number prints as before, which
-matches a Python int below 1e21: JavaScript cannot tell Python's `49.0` from `49`. */
+digits, but CPython's `repr` switches to exponent notation when the decimal exponent is below -4
+OR at or above 16 -- JavaScript's `String(v)` switches below 1e-6 and at 1e21 -- and pads a single
+digit exponent to two (`5e-05`, not `5e-5`, and `1e+16`, never `1e16`). An integer-valued number
+prints as before, which matches a Python int below 1e21: JavaScript cannot tell Python's `49.0`
+from `49`. The upper bound is not cosmetic: `1e16` is `10000000000000000` in JavaScript and
+`1e+16` in Python, and that text is what the tokenizer sees. */
function pyNumber(v: number): string {
const [mant, exp] = v.toExponential().split("e");
const e = Number(exp);
- return e >= -4 ? String(v) : `${mant}e-${String(-e).padStart(2, "0")}`;
+ if (e >= -4 && e < 16) return String(v);
+ return `${mant}e${e < 0 ? "-" : "+"}${String(Math.abs(e)).padStart(2, "0")}`;
}
/** Python `json.dumps(v, ensure_ascii=False)` replica: separators (", ", ": "),
unicode raw, unknown types fall back to undefined (caller applies str()). */
diff --git a/laya-ts/tests/parity-sequence.test.ts b/laya-ts/tests/parity-sequence.test.ts
index ceecff87..cc1a73bc 100644
--- a/laya-ts/tests/parity-sequence.test.ts
+++ b/laya-ts/tests/parity-sequence.test.ts
@@ -24,8 +24,9 @@ describe("common parity", () => {
});
it("formats non-integer numbers like Python's float repr", () => {
// Expected strings are Python's json.dumps({"x": v}, ensure_ascii=False). Python switches
- // to exponent notation below 1e-4 and pads the exponent to two digits; JavaScript's
- // Number#toString switches only below 1e-6 and never pads, so the model read other tokens.
+ // to exponent notation below 1e-4 OR at 1e16 and above, and pads the exponent to two digits;
+ // JavaScript's Number#toString switches only below 1e-6 and never pads, so the model read
+ // other tokens. The upper bound is the one a state carrying money or an id reaches.
const cases: [number, string][] = [
[0.5, '{"x": 0.5}'],
[0.0001, '{"x": 0.0001}'],
@@ -35,12 +36,19 @@ describe("common parity", () => {
[-2.5e-8, '{"x": -2.5e-08}'],
[1.5e-10, '{"x": 1.5e-10}'],
[5e-324, '{"x": 5e-324}'],
+ [1e16, '{"x": 1e+16}'],
+ [1.5e16, '{"x": 1.5e+16}'],
+ [-1e16, '{"x": -1e+16}'],
+ [1e21, '{"x": 1e+21}'],
+ [1.2345678901234568e17, '{"x": 1.2345678901234568e+17}'],
+ [1e308, '{"x": 1e+308}'],
];
for (const [v, want] of cases) expect(serializeState({ x: v })).toBe(want);
expect(renderOptions({ t: "score", ins: "x", crit: [0.00005] })).toEqual(["level 0: 5e-05"]);
- // an integer-valued number reads as a Python int, which keeps plain digits
+ // Below 1e16 the fixed form is Python's; an integer-valued number reads as a Python int.
expect(serializeState({ n: 49 })).toBe('{"n": 49}');
- expect(serializeState({ n: 1e16 })).toBe('{"n": 10000000000000000}');
+ expect(serializeState({ n: 1e15 })).toBe('{"n": 1000000000000000}');
+ expect(serializeState(1e16)).toBe("1e+16");
});
it("truncateLeft keeps tail of state (py parity)", () => {
const tok = {