Skip to content

fix: negate the result when folding 0 - x - #123

Merged
amol- merged 1 commit into
posit-dev:mainfrom
Sanjays2402:fix/fold-zeros-subtract-negate
Aug 10, 2026
Merged

fix: negate the result when folding 0 - x#123
amol- merged 1 commit into
posit-dev:mainfrom
Sanjays2402:fix/fold-zeros-subtract-negate

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Closes #108

fold_zeros returned the right operand unchanged for a Subtract whose left operand is the literal 0, so 0 - x folded to x and the sign flip was dropped. The folded expression is now negated.

The existing test_fold_zeros_subtract_left only checked result.op().value, which is 5 either way, so it passed on the buggy code; it now asserts the computed value, and a column case is added alongside it. Both fail without the source change and pass with it (tests/test_optimizer.py: 2 failed / 18 passed before, 20 passed after).

This change was prepared with AI assistance; the regression test was run locally and fails without the fix.

Optimizer.fold_zeros returned the right operand unchanged for a Subtract
whose left operand is the literal 0, so 0 - x folded to x and dropped the
sign flip. The folded expression is now negated.

Extended the existing test_fold_zeros_subtract_left assertion to check the
computed value and added a column case, both of which fail without the fix.

Closes posit-dev#108
@amol-

amol- commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks for tackling this! I'll review it as soon as I can and come back to you 🙏

@amol-

amol- commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

seems good! merging it

@amol-
amol- merged commit 0d6b80a into posit-dev:main Aug 10, 2026
7 of 8 checks passed
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.

Optimizer.fold_zeros folds 0 - x to x instead of -x

2 participants