Add assert.approx_equal for epsilon-based float assertions#3669
Conversation
Co-authored-by: Dhilan Shah <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
⏭️ Performance benchmarks were skippedPerf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to To run them on this PR, do any of the following, then push a commit (or re-run CI):
|
Co-authored-by: Dhilan Shah <[email protected]>
Binary size checks passed✅ 7 passed
Generated by |
Co-authored-by: Dhilan Shah <[email protected]>
Pull Request Template
Issue Reference
Changes
Adds
assert.approx_equal(actual, expected, epsilon)to the BAML test stdlib for epsilon-based float comparisons.Testing
Screenshots
Not applicable.
PR Checklist
Additional Notes
The error
Before this change, the BAML test stdlib did not define
assert.approx_equal, so this smallest repro failed:test "approx_equal_missing" { assert.approx_equal(float.parse("0.1") + float.parse("0.2"), float.parse("0.3"), 0.000000000000001) }Command run against
origin/canary:cargo run -p baml_cli -- test --from /tmp/baml-approx-repro-srcOutput:
Root cause
baml_language/crates/baml_builtins2/baml_std/assert/assert.bamldefined the test stdlib assertionsis_true,not_null,equal, andcontains, but had no epsilon-based float comparison primitive.assert.equal(actual, expected)uses exact equality, so float tests for non-exact IEEE 754 results had to inline(actual - expected).abs() <= epsilonmanually.The fix
Added
assert.approx_equal(actual: float, expected: float, epsilon: float) -> nullinbaml_language/crates/baml_builtins2/baml_std/assert/assert.baml. It computesdifference = actual - expected, thendelta = difference.abs(), and panics unlessdelta <= epsilon. The panic message includesactual,expected,epsilon, and the observeddelta.I also added BAML float tests for:
float.parse("0.1") + float.parse("0.2")vsfloat.parse("0.3")delta == epsilonSnapshots were updated for the assert stdlib compiler phases, the BAML source bytecode, and the CLI package listing.
Verification
Commands run after the fix:
Relevant passing output showing the same repro now succeeds in
baml_src/ns_floats/floats.baml:Passing output: