Skip to content

feat(eval): fold ^ before multiply - #9

Open
JustNak wants to merge 1 commit into
omacom:masterfrom
JustNak:feat/power-operator
Open

feat(eval): fold ^ before multiply#9
JustNak wants to merge 1 commit into
omacom:masterfrom
JustNak:feat/power-operator

Conversation

@JustNak

@JustNak JustNak commented Aug 26, 2026

Copy link
Copy Markdown

Why

The keypad is four-function. Keyboard users still expect Shift+6 to raise a number to a power. 2 ^ 2 should be 4.

Scope

  • Backend::evaluateTokens folds ^ right-to-left, then × ÷, then + .
  • pressKey and isOperator treat ^ as an operator, so dangling-operator replacement and chaining keep working.
  • Main.qml accepts ^ from the keyboard. The 4×5 keypad is unchanged.
  • 0 ^ 0 and a negative base with a non-integer exponent return Error.
  • Percent with pending ^ is x÷100, same as × and ÷.
  • README.md documents the key and the fold order.
  • Tests in calculatesPowers, percentOfRunningTotal, and evaluatesTokens.

Tradeoffs

  • No keypad button. The face stays 4×5. Power is keyboard-only.
  • Right-associative ^, so 2 ^ 3 ^ 2 is 512. A left-associative fold would print 64 and surprise anyone who writes math.
  • No parser rewrite. The expression stays an alternating token list with one more fold pass.

Blast Radius

Keyboard ^ is new input. Existing + × ÷ paths still run after the power pass. Percent of running total still applies only to pending + or .

Verification

  • ./bin/test in this tree. 20 passed, 0 failed, including 2 ^ 2, mixed precedence, right-associativity, 0 ^ 0, (-2) ^ 0.5, and 2 ^ 10%.
  • ./bin/build produced build/omacalc.
  • QML key routing is not under Qt Test. The regex now includes ^. pressKey("^") is what the tests drive.

The keyboard sends ^. evaluateTokens walks it right-to-left so
2 ^ 3 ^ 2 is 512, then × and ÷, then + and −.

0^0 and a negative base with a non-integer exponent return Error.
The keypad is unchanged.
@greptile-apps

greptile-apps Bot commented Aug 26, 2026

Copy link
Copy Markdown

Greptile Summary

The PR adds keyboard-entered exponentiation with right-associative precedence while retaining the existing alternating-token evaluator design.

  • Routes ^ from QML keyboard input into the backend operator lifecycle.
  • Folds powers before multiplication, division, addition, and subtraction.
  • Documents exponentiation and adds backend coverage for precedence, associativity, percent handling, chaining, and invalid powers.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/backend.cpp Adds ^ to operator state handling and implements guarded, right-associative power folding before the existing arithmetic passes.
src/Main.qml Extends the focused keyboard handler to forward the ^ character without changing the on-screen keypad.
tests/tst_omacalc.cpp Covers exponentiation precedence, associativity, chaining, invalid domains, and percent behavior.
README.md Documents keyboard exponentiation, evaluation order, and percent semantics with a pending power operator.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    A[Keyboard input: ^] --> B[Backend::pressKey]
    B --> C[pressOperator]
    C --> D[Alternating expression tokens]
    D --> E[Fold powers right-to-left]
    E --> F[Fold multiplication and division left-to-right]
    F --> G[Fold addition and subtraction]
    G --> H[Formatted result or Error]
Loading

Reviews (2): Last reviewed commit: "feat(eval): fold ^ before multiply" | Re-trigger Greptile

@JustNak

JustNak commented Aug 26, 2026

Copy link
Copy Markdown
Author

Closing this in favor of reviewing on the fork first: JustNak#1

@JustNak JustNak closed this Aug 26, 2026
@JustNak JustNak reopened this Aug 26, 2026
@curtislmartin

Copy link
Copy Markdown

I've been wanting this too. Had the same idea, keep as keyboard only.
Hope it makes it in!

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