Skip to content

Commit 1edba4f

Browse files
anandgupta42claude
andcommitted
fix(answer-commit): keep decimals more than two ulps from a six-place rounding
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 9c55371 commit 1edba4f

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,17 @@ export function renderAnswer(payload: unknown): string {
215215
// altimate_change start - render the requested format before applying the shared size cap
216216
const { columns, values, precision, format = "markdown" } = args.rows
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.
218+
// Two units in the last place of a double: arithmetic artifacts (0.1 + 0.2) sit within it, genuine 15+ digit decimals do not.
219+
const DOUBLE_ARTIFACT_RELATIVE_ERROR = Number.EPSILON * 2
218220
const isNoise = (value: number) => {
219221
const decimal = new Decimal(value)
220222
if (decimal.decimalPlaces() <= 6) return false
221223
const rounded = decimal.toDecimalPlaces(6)
222224
// Only a double-precision artifact or the exact float32 widening of the rounded value qualifies.
223-
return Math.fround(rounded.toNumber()) === value || decimal.minus(rounded).abs().lte(decimal.abs().times("1e-15"))
225+
return (
226+
Math.fround(rounded.toNumber()) === value ||
227+
decimal.minus(rounded).abs().lte(decimal.abs().times(DOUBLE_ARTIFACT_RELATIVE_ERROR))
228+
)
224229
}
225230
const scales = columns.map((_, index) =>
226231
values.reduce((scale, cells) => {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,15 @@ describe("answer_commit display precision", () => {
436436
)
437437
})
438438

439+
test("a decimal a few ulps from a six-place rounding is kept", () => {
440+
const payload = {
441+
basis: recordBasis,
442+
answer_only: true,
443+
rows: { columns: ["Value"], values: [[1.000000000000001], [2.1]] },
444+
}
445+
expect(AnswerCommit.renderAnswer(payload)).toBe("| Value |\n|---|\n| 1.000000000000001 |\n| 2.100000000000000 |")
446+
})
447+
439448
test("a column of only float-noise cells renders each in its own form", () => {
440449
const payload = {
441450
basis: recordBasis,

0 commit comments

Comments
 (0)