Skip to content

Match float literals to float32 comparisons - #983

Open
deepakganesh78 wants to merge 1 commit into
expr-lang:masterfrom
deepakganesh78:fix/issue609-float32-literals
Open

Match float literals to float32 comparisons#983
deepakganesh78 wants to merge 1 commit into
expr-lang:masterfrom
deepakganesh78:fix/issue609-float32-literals

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #609

Root cause

Expr compiles floating-point literals as float64. Comparing one directly with a float32 operand therefore compares the rounded float32 value with the original float64 literal, unlike Go's conversion of the untyped literal to float32.

Fix

After type checking, patch float literals used directly in comparisons with float32 operands into typed float32 constants. This handles either operand order and signed literals without changing mixed float32/float64 arithmetic.

Tests

  • go test .\checker . -run 'Test(Float32LiteralComparisons|Check)' -count=1
  • go test ./...
  • go test -tags=expr_debug -run=TestDebugger -v ./vm
  • go vet ./...

The regression test covers optimized and unoptimized compilation, both operand orders, signed literals, relational and inequality comparisons, and verifies float64 behavior remains unchanged.

go test -race . was not available on this Windows host because the installed Go toolchain has CGO disabled.

Float literals are normally compiled as float64 values, which makes comparisons against float32 operands use different rounded values. Patch literals in those comparisons to float32 after type checking so optimized and unoptimized programs follow Go's literal conversion behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@deepakganesh78
deepakganesh78 force-pushed the fix/issue609-float32-literals branch from 29fc629 to 75aa119 Compare July 26, 2026 11:38

@sanmaxdev sanmaxdev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified this fixes #609. Reproduced F32 == 12.34 returning false on master, and it returns true on this branch. The patcher approach is what was requested in the issue thread, and it correctly handles both operand orders plus signed literals without touching mixed float32/float64 arithmetic.

Verification run on the PR head:

  • go test ./... (full suite, no failures)
  • go vet ./... (clean)
  • go test -race -count=1 ./checker . (green)

One test gap: isComparisonOperator returns true for <, >, and >=, but the table in TestFloat32LiteralComparisons only exercises ==, !=, and <=. Adding rows for the remaining three would cover every branch of the helper and guard against a future regression that narrows the accepted operator set.

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.

Cast IntegerNode to float32 in operations like f32 == 1.5

2 participants