Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions laya-ts/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()). */
Expand Down
16 changes: 12 additions & 4 deletions laya-ts/tests/parity-sequence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'],
Expand All @@ -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 = {
Expand Down
Loading