Skip to content

Add assert.approx_equal for epsilon-based float assertions#3669

Draft
ATX24 wants to merge 3 commits into
canaryfrom
cursor/baml-assert-approx-equal-bf50
Draft

Add assert.approx_equal for epsilon-based float assertions#3669
ATX24 wants to merge 3 commits into
canaryfrom
cursor/baml-assert-approx-equal-bf50

Conversation

@ATX24

@ATX24 ATX24 commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Pull Request Template

Issue Reference

  • This PR fixes/closes #[issue number]

Changes

Adds assert.approx_equal(actual, expected, epsilon) to the BAML test stdlib for epsilon-based float comparisons.

Testing

  • Unit tests added/updated
  • Manual testing performed
  • Tested in Cursor Cloud Linux environment

Screenshots

Not applicable.

PR Checklist

  • I have read and followed the contributing guidelines
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

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-src

Output:

warning: using the internal BAML toolchain binary directly is not recommended. Use `baml` instead.
     Loading /tmp/baml-approx-repro-src/main.baml
    Checking 1 file(s)
error: unresolved name: approx_equal
   ╭─[ main.baml:1:1 ]
   │
 1 │ test "approx_equal_missing" {
   │ │ 
   │ ╰─ unresolved name: approx_equal
   │ 
   │ Note: Error code: E0003
───╯

error: unresolved name: approx_equal
   ╭─[ main.baml:1:30 ]
   │
 1 │ ╭─▶ test "approx_equal_missing" {
 2 │ ├─▶     assert.approx_equal(float.parse("0.1") + float.parse("0.2"), float.parse("0.3"), 0.000000000000001)
   │ │                                                                                                             
   │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────── unresolved name: approx_equal
   │     
   │     Note: Error code: E0003
───╯

Root cause

baml_language/crates/baml_builtins2/baml_std/assert/assert.baml defined the test stdlib assertions is_true, not_null, equal, and contains, 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() <= epsilon manually.

The fix

Added assert.approx_equal(actual: float, expected: float, epsilon: float) -> null in baml_language/crates/baml_builtins2/baml_std/assert/assert.baml. It computes difference = actual - expected, then delta = difference.abs(), and panics unless delta <= epsilon. The panic message includes actual, expected, epsilon, and the observed delta.

I also added BAML float tests for:

  • a non-exact runtime float expression: float.parse("0.1") + float.parse("0.2") vs float.parse("0.3")
  • the inclusive boundary where delta == epsilon

Snapshots were updated for the assert stdlib compiler phases, the BAML source bytecode, and the CLI package listing.

Verification

Commands run after the fix:

PATH="$HOME/.local/bin:$PATH" cargo test -p baml_tests --test baml_src baml_test -- --nocapture

Relevant passing output showing the same repro now succeeds in baml_src/ns_floats/floats.baml:

PASS ::float_approx_equal_accepts_non_exact_result
PASS ::float_approx_equal_accepts_delta_equal_to_epsilon
Finished 1616 passed, 0 failed, 1616 total in 42s
test baml_test ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 102.63s
PATH="$HOME/.local/bin:$PATH" cargo test -p baml_tests __assert_std__
PATH="$HOME/.local/bin:$PATH" cargo test -p baml_tests --test baml_src bytecode
PATH="$HOME/.local/bin:$PATH" cargo test -p baml_cli render_assert_package_listing

Passing output:

test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 1570 filtered out; finished in 1.14s

test bytecode ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 30.45s

test describe_command_tests::render_assert_package_listing ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 162 filtered out; finished in 0.05s
Open in Web Open in Cursor 

@vercel

vercel Bot commented Jun 3, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jun 3, 2026 7:41pm
promptfiddle Ready Ready Preview, Comment Jun 3, 2026 7:41pm
promptfiddle2 Ready Ready Preview, Comment Jun 3, 2026 7:41pm

Request Review

@coderabbitai

coderabbitai Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 7d6ce48d-a439-44e0-80c7-17f167a6c1bf

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch cursor/baml-assert-approx-equal-bf50

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

Binary size checks passed

7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
baml-cli Linux 🔒 17.6 MB 7.5 MB file 24.1 MB -6.4 MB (-26.8%) OK
packed-program Linux 🔒 12.8 MB 5.4 MB file 15.1 MB -2.3 MB (-15.0%) OK
baml-cli macOS 🔒 13.5 MB 6.5 MB file 18.2 MB -4.7 MB (-26.0%) OK
packed-program macOS 🔒 9.8 MB 4.7 MB file 11.5 MB -1.7 MB (-14.9%) OK
baml-cli Windows 🔒 14.5 MB 6.7 MB file 19.6 MB -5.1 MB (-26.2%) OK
packed-program Windows 🔒 10.3 MB 4.8 MB file 12.2 MB -1.9 MB (-15.5%) OK
bridge_wasm WASM 11.8 MB 🔒 3.3 MB gzip 3.9 MB -524.7 KB (-13.6%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants