Skip to content

Commit ccc03c9

Browse files
anandgupta42claude
andcommitted
fix(answer-commit): keep the rounded scale of recognized float noise
- A recognized float artifact now contributes the scale of the shortest value it is an artifact of, so a column mixing it with coarser cells keeps the intended digits. - A column containing only noise cells keeps its values as given. - Add a regression test. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5b8513d commit ccc03c9

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

‎packages/opencode/src/altimate/tools/answer-commit.ts‎

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,29 +217,34 @@ export function renderAnswer(payload: unknown): string {
217217
// Float noise: more than 6 decimals, yet either a double-precision artifact of, or the exact float32 widening of, a value with at most 6 decimals (0.30000000000000004, 18.440000534057617). Genuine long decimals (1234567.8901234) are not.
218218
// Two units in the last place of a double: arithmetic artifacts (0.1 + 0.2) sit within it, genuine 15+ digit decimals do not.
219219
const DOUBLE_ARTIFACT_RELATIVE_ERROR = Number.EPSILON * 2
220-
const isNoise = (value: number) => {
220+
// Returns the shortest (at most 6 decimal) value the noisy cell is an artifact of, or undefined for genuine values.
221+
const artifactOf = (value: number) => {
221222
const decimal = new Decimal(value)
222-
if (decimal.decimalPlaces() <= 6) return false
223-
const rounded = decimal.toDecimalPlaces(6)
224-
// Only a double-precision artifact or the exact float32 widening of the rounded value qualifies.
225-
return (
226-
Math.fround(rounded.toNumber()) === value ||
227-
decimal.minus(rounded).abs().lte(decimal.abs().times(DOUBLE_ARTIFACT_RELATIVE_ERROR))
228-
)
223+
if (decimal.decimalPlaces() <= 6) return undefined
224+
for (let places = 0; places <= 6; places++) {
225+
const rounded = decimal.toDecimalPlaces(places)
226+
// Only a double-precision artifact or the exact float32 widening of the rounded value qualifies.
227+
if (
228+
Math.fround(rounded.toNumber()) === value ||
229+
decimal.minus(rounded).abs().lte(decimal.abs().times(DOUBLE_ARTIFACT_RELATIVE_ERROR))
230+
)
231+
return rounded
232+
}
233+
return undefined
229234
}
235+
// Noise contributes the scale of the short value it is an artifact of, never its raw digits.
236+
const places = (value: number) => (artifactOf(value) ?? new Decimal(value)).decimalPlaces()
237+
// A column with no ordinary numeric cell keeps its values as given.
238+
const onlyNoise = columns.map((_, index) => {
239+
const numbers = values.map((cells) => cells[index]).filter((cell): cell is number => typeof cell === "number")
240+
return numbers.length > 0 && numbers.every((cell) => artifactOf(cell) !== undefined)
241+
})
230242
const scales = columns.map((_, index) =>
231243
values.reduce((scale, cells) => {
232244
const value = cells[index]
233-
return typeof value === "number" && !isNoise(value)
234-
? Math.max(scale, new Decimal(value).decimalPlaces())
235-
: scale
245+
return typeof value === "number" ? Math.max(scale, places(value)) : scale
236246
}, 0),
237247
)
238-
const inferred = (value: number, index: number) => {
239-
const decimal = new Decimal(value)
240-
if (!isNoise(value)) return decimal.toFixed(scales[index]!)
241-
return scales[index]! > 0 ? decimal.toFixed(scales[index]!) : decimal.toFixed()
242-
}
243248
const row = (cells: (string | number | null)[]) => {
244249
if (format === "markdown") return `| ${cells.map(tableCell).join(" | ")} |`
245250
if (format === "lines") return cells.map(String).join(" ")
@@ -260,7 +265,7 @@ export function renderAnswer(payload: unknown): string {
260265
const places =
261266
precision && Object.hasOwn(precision, columns[index]!) ? precision[columns[index]!] : undefined
262267
if (typeof value !== "number") return value
263-
return places !== undefined ? value.toFixed(places) : inferred(value, index)
268+
return places !== undefined ? value.toFixed(places) : (onlyNoise[index] ? new Decimal(value).toFixed() : new Decimal(value).toFixed(scales[index]!))
264269
}),
265270
),
266271
),

‎packages/opencode/test/altimate/tools/answer-commit.test.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,22 @@ describe("answer_commit display precision", () => {
445445
expect(AnswerCommit.renderAnswer(payload)).toBe("| Value |\n|---|\n| 1.000000000000001 |\n| 2.100000000000000 |")
446446
})
447447

448-
test("a column of only float-noise cells renders each in its own form", () => {
448+
test("a column of only float-noise cells uses the scale of their rounded values", () => {
449449
const payload = {
450450
basis: recordBasis,
451451
answer_only: true,
452452
rows: { columns: ["Value"], values: [[0.30000000000000004], [7]], format: "lines" as const },
453453
}
454-
expect(AnswerCommit.renderAnswer(payload)).toBe("0.30000000000000004\n7")
454+
expect(AnswerCommit.renderAnswer(payload)).toBe("0.3\n7.0")
455+
})
456+
457+
test("recognized noise keeps its rounded scale beside coarser cells", () => {
458+
const payload = {
459+
basis: recordBasis,
460+
answer_only: true,
461+
rows: { columns: ["Value"], values: [[18.440000534057617], [1.2]], format: "lines" as const },
462+
}
463+
expect(AnswerCommit.renderAnswer(payload)).toBe("18.44\n1.20")
455464
})
456465

457466
test("genuine long decimals are preserved beside clean cells", () => {

0 commit comments

Comments
 (0)