Skip to content

Fix unary minus directly after ^ in the bundled Javaluator - #198

Merged
Travja merged 1 commit into
devfrom
fix/unary-minus-after-exponent
Nov 18, 2026
Merged

Travja merged 1 commit into
devfrom
fix/unary-minus-after-exponent

Conversation

@Travja

@Travja Travja commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Fixes #197

Problem

A unary minus directly after ^ gave a wrong number when another operator came before it. With x=30, x * 2^-1 returned -900 instead of 15, and a bare 2^-1 was rejected. Both the string evaluator (Evaluator.eval / DoubleEvaluator.evaluate) and the compiled one (Evaluator.compile) were affected, because they share one parser.

Root cause

In AbstractEvaluator.evaluate, the shunting-yard loop for an incoming operator pops pending operators of higher precedence. A unary minus (precedence 3) therefore popped a pending ^ (precedence 4) before the ^ had its right operand, so ^ consumed the wrong values (or too few).

Fix

A prefix operator has no left operand and must never pop anything, so the pop loop is skipped for unary operators. This is a one-line change. Every other expression takes the same path as before. Unary minus still binds looser than ^, so -2^2 is still -4 and 2^-3^2 is 2^-(3^2). Excel style (NEGATE_HIGH) is unchanged.

Tests

  • New NegativeExponentTest runs the same cases through both evaluators: 2^-1, x * 2^-1, 2^-x, -2^2, 2^--1, 2^-3^2, inside functions and brackets, and the Excel style. It failed before the fix, with the issue's -900, and passes now.
  • CompiledExpressionTest had a test that pinned 2^-1, -d^-x and d^-1 as rejected. The issue prefers correct evaluation, so it now checks that the compiled and string evaluators agree on those expressions.
  • Full codex-api suite: 407 tests, 0 failures.

Downstream

magemonkeystudio/divinity has a guard for this bug (NEGATIVE_EXPONENT in CombatFormulas). It can be removed once this ships, along with raising its MIN_CORE_VERSION to the Codex build that has the fix.


Generated by Claude Code

A prefix operator has no left operand, so it must never pop a pending
operator off the stack. The shunting-yard loop let unary minus (precedence 3)
pop a pending ^ (precedence 4), which then ran before its right operand was
read: `x * 2^-1` returned -900 for x=30 and a bare `2^-1` was rejected.

Skip the pop loop for unary operators. Both the string and the compiled
evaluator share this parser. The existing test that pinned the rejection of
`2^-1` now checks that the compiled and string evaluators agree.

Fixes #197

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FaEyTsM3SWu5hP2sUDjCjR
@Travja Travja added the build:patch label Nov 18, 2026 — with Claude
@Travja
Travja merged commit c558a35 into dev Nov 18, 2026
2 of 3 checks passed
@Travja
Travja deleted the fix/unary-minus-after-exponent branch November 18, 2026 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Evaluator / Javaluator computes x * 2^-1 wrongly instead of rejecting it

2 participants