Skip to content

Commit ad7331a

Browse files
fix(formula): decimal text is judged against the number range before it is scaled (#34570)
Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:`toScaledDecimal` 先按数值范围判定十进制文本,再做缩放。超出范围的文本读为无值,低于最小数的文本读为零,零不再按其指数缩放;范围内每一位仍精确保留。 - 为什么改:同一个值以文本和以数字传入时,判定结果必须一致。 - 风险与回滚:范围内的读取不变,数字和 bigint 的读取不变,`divideToPlaces` 与 `formatScaledDecimal` 不变。回滚即 revert 本 PR。 - 先看:`packages/formula/src/home-amount-arithmetic.ts` 的 `toScaledDecimal`。 - 需要你做的:无,席位落地。 ## What changes `toScaledDecimal` (`@objectstack/formula`) judges decimal text against the number range before it scales it, the way the same value is judged as a number: - Text whose number is not finite spells no decimal and reads `undefined`, as a non-finite number already did. - Text whose number is zero because it is below the smallest number reads as zero, as that number already did. - A zero is never scaled by its exponent. Inside the range every digit is still kept exactly. The home-amount projection treats an amount that spells no decimal as before: it leaves the value to the record validator. ## Tests `packages/formula/src/home-amount-arithmetic.test.ts`: text above the range reads `undefined`; text below the smallest number reads zero, either sign; a zero with an exponent reads plain zero; in-range text keeps every digit (control). The implementer's suite, gate and ablation readings were read by the security#1 seat (brief 6934902629); this PR is opened by the security#2 seat after that seat's clock-out. --- _Generated by [Claude Code](https://claude.ai/code/session_01QSeKYn5CmmdShCJifm6WYj)_ <!-- signed: security#2 · os-justin · session_01QSeKYn5CmmdShCJifm6WYj --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent 066177b commit ad7331a

3 files changed

Lines changed: 105 additions & 4 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@objectstack/formula': patch
3+
---
4+
5+
fix(formula): decimal text is judged against the number range before it is scaled
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`toScaledDecimal` now judges decimal text against the number range before it scales it, the way the same value is judged as a number:
11+
12+
- Text whose number is not finite (`'1e400'`, `'1.8e308'`) spells no decimal and reads `undefined`, as a non-finite number already did.
13+
- Text whose number is zero because it is below the smallest number (`'1e-400'`) reads as zero, as that number already did.
14+
- A zero is never scaled by its exponent; one written with an exponent below the smallest number's (`'0e-325'`) reads as plain zero.
15+
16+
Inside the range every digit is still kept exactly, numbers and bigints are read as before, and `divideToPlaces` and `formatScaledDecimal` are unchanged. The home-amount projection treats an amount that spells no decimal as before: it leaves the value to the record validator, which refuses a non-finite number as `invalid_number`.

‎packages/formula/src/home-amount-arithmetic.test.ts‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,62 @@ describe('the home-amount arithmetic exported from @objectstack/formula', () =>
6262
expect(documentDateOf(20260315)).toBeUndefined();
6363
});
6464
});
65+
66+
describe('decimal text is judged against the number range before it is scaled', () => {
67+
const ZERO = { digits: 0n, scale: 0 };
68+
69+
it('beyond the largest number it spells none, as a non-finite number does', () => {
70+
for (const text of ['1e400', '-1e400', '1.8e308', '+.5E+400', `1${'0'.repeat(400)}`]) {
71+
expect(toScaledDecimal(text), text).toBeUndefined();
72+
expect(Number.isFinite(Number(text)), text).toBe(false);
73+
}
74+
expect(toScaledDecimal(Number('1e400'))).toBeUndefined();
75+
});
76+
77+
it('below the smallest number it is zero, as that number is', () => {
78+
for (const text of ['1e-400', '-1e-400', '2e-324', '+.5e-400', `0.${'0'.repeat(400)}1`]) {
79+
expect(toScaledDecimal(text), text).toEqual(ZERO);
80+
expect(Number(text) === 0, text).toBe(true);
81+
}
82+
expect(toScaledDecimal(Number('1e-400'))).toEqual(ZERO);
83+
expect(formatScaledDecimal(divideToPlaces(toScaledDecimal('1e-400')!, { digits: 1n, scale: 0 }, 2))).toBe('0.00');
84+
});
85+
86+
it('keeps every digit inside the range, at both ends', () => {
87+
const largest = { digits: 17976931348623157n * 10n ** 292n, scale: 0 };
88+
expect(toScaledDecimal('1.7976931348623157e308')).toEqual(largest);
89+
expect(toScaledDecimal(Number.MAX_VALUE)).toEqual(largest);
90+
expect(toScaledDecimal('5e-324')).toEqual({ digits: 5n, scale: 324 });
91+
expect(toScaledDecimal(Number.MIN_VALUE)).toEqual({ digits: 5n, scale: 324 });
92+
expect(toScaledDecimal('1e300')).toEqual({ digits: 10n ** 300n, scale: 0 });
93+
expect(toScaledDecimal('-1.5e-300')).toEqual({ digits: -15n, scale: 301 });
94+
// A bigint is its own digits, however many.
95+
expect(toScaledDecimal(10n ** 400n)).toEqual({ digits: 10n ** 400n, scale: 0 });
96+
});
97+
98+
it('never scales a zero by its exponent', () => {
99+
expect(toScaledDecimal('0.00')).toEqual({ digits: 0n, scale: 2 });
100+
expect(toScaledDecimal('0e-5')).toEqual({ digits: 0n, scale: 5 });
101+
expect(toScaledDecimal('0e-324')).toEqual({ digits: 0n, scale: 324 });
102+
expect(toScaledDecimal('0e-325')).toEqual(ZERO);
103+
expect(toScaledDecimal('-0e5')).toEqual(ZERO);
104+
});
105+
106+
it('reads and converts in time bounded by the text, whatever its exponent says', () => {
107+
const texts = [
108+
'1e30000000', '-1e30000000', '+.5E+30000000', '0e30000000',
109+
'1e-30000000', '+1e-30000000', '0e-30000000',
110+
`1e-${'9'.repeat(400)}`, `1e${'9'.repeat(400)}`,
111+
];
112+
const heapBefore = process.memoryUsage().heapUsed;
113+
const started = performance.now();
114+
const read = texts.map((text) => toScaledDecimal(text));
115+
const converted = read.map((value) => value && formatScaledDecimal(divideToPlaces(value, { digits: 1n, scale: 0 }, 2)));
116+
const elapsed = performance.now() - started;
117+
const allocated = process.memoryUsage().heapUsed - heapBefore;
118+
expect(read).toEqual([undefined, undefined, undefined, ZERO, ZERO, ZERO, ZERO, ZERO, undefined]);
119+
expect(converted).toEqual([undefined, undefined, undefined, '0.00', '0.00', '0.00', '0.00', '0.00', undefined]);
120+
expect(elapsed).toBeLessThan(200);
121+
expect(allocated).toBeLessThan(64 * 2 ** 20);
122+
});
123+
});

‎packages/formula/src/home-amount-arithmetic.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,27 @@ export interface ScaledDecimal {
2626

2727
const DECIMAL_SPELLING = /^([+-]?)(\d*)(?:\.(\d*))?(?:[eE]([+-]?\d+))?$/;
2828

29+
/**
30+
* The exponent of the smallest number, `Number.MIN_VALUE` (`5e-324`): a zero
31+
* written with an exponent below it reads as plain zero.
32+
*/
33+
const SMALLEST_NUMBER_EXPONENT = -324;
34+
2935
/**
3036
* The exact decimal a stored or written number spells, or `undefined` when it
3137
* spells none. A JavaScript number is read through its shortest round-trip
3238
* spelling (`String(0.1389)` is `'0.1389'`), which is the decimal the writer
3339
* wrote; a string (a driver answering an exact-decimal column as text) is read
34-
* as written. Exponent spellings are accepted.
40+
* as written. Exponent spellings are accepted. A bigint is its own digits.
41+
*
42+
* Decimal text is judged against the number range before it is scaled, the
43+
* way the same value is judged as a number: text whose number is not finite
44+
* spells none, as a non-finite number does (`'1e400'`, like `1e400`), and text
45+
* whose number is zero is zero, as that number is (`'1e-400'`, like `1e-400`).
46+
* A zero is never scaled by its exponent, and one written with an exponent
47+
* below the smallest number's is plain zero. Inside the range every digit is
48+
* kept exactly, so the work is bounded by the length of the text, whatever
49+
* its exponent says.
3550
*/
3651
export function toScaledDecimal(value: unknown): ScaledDecimal | undefined {
3752
let text: string;
@@ -49,8 +64,18 @@ export function toScaledDecimal(value: unknown): ScaledDecimal | undefined {
4964
if (!m) return undefined;
5065
const [, sign, int = '', frac = '', exp] = m;
5166
if (int === '' && frac === '') return undefined;
52-
let digits = BigInt(`${int}${frac}` || '0');
53-
let scale = frac.length - (exp === undefined ? 0 : Number(exp));
67+
// A long exponent reads ±Infinity, which every comparison below handles.
68+
const exponent = exp === undefined ? 0 : Number(exp);
69+
if (!/[1-9]/.test(int) && !/[1-9]/.test(frac)) {
70+
return { digits: 0n, scale: exponent < SMALLEST_NUMBER_EXPONENT ? 0 : Math.max(0, frac.length - exponent) };
71+
}
72+
if (typeof value === 'string') {
73+
const number = Number(text);
74+
if (!Number.isFinite(number)) return undefined;
75+
if (number === 0) return { digits: 0n, scale: 0 };
76+
}
77+
let digits = BigInt(`${int}${frac}`);
78+
let scale = frac.length - exponent;
5479
if (scale < 0) {
5580
digits *= 10n ** BigInt(-scale);
5681
scale = 0;
@@ -76,7 +101,8 @@ function divideRoundHalfAwayFromZero(numerator: bigint, denominator: bigint): bi
76101
/**
77102
* `amount ÷ rate`, rounded once, half away from zero, to `places` decimals —
78103
* parameter 3 as arithmetic. Both operands are exact; so is every
79-
* intermediate.
104+
* intermediate, so the work grows with the operands' scales — which
105+
* {@link toScaledDecimal} bounds by the length of the text it read.
80106
*/
81107
export function divideToPlaces(amount: ScaledDecimal, rate: ScaledDecimal, places: number): ScaledDecimal {
82108
// (a / 10^as) / (r / 10^rs) × 10^places = a × 10^(rs + places) / (r × 10^as)

0 commit comments

Comments
 (0)