Skip to content

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

Closed

Description

@Travja

A unary minus directly after ^ is mishandled when another operator comes before it. A bare 2^-1 is rejected, but x * 2^-1 is accepted and returns a wrong number.

Repro

Codex 1.3.0-dev.2 (studio.magemonkey.codex.util.eval.Evaluator, published 1.3.0-SNAPSHOT build 20261115.232732-2):

Expression Variables Result Expected
2^-1 defense IllegalArgumentException "Cannot compile '2^-1': malformed expression" 0.5 or rejection
defense * 2^-1 defense = 30 -900.0 15.0
defense * 2^(-1) defense = 30 15.0 15.0
2^(-1) defense = 30 0.5 0.5
1 - -1 defense = 30 2.0 2.0

Unary minus elsewhere is fine. The string evaluator (Evaluator.eval / DoubleEvaluator.evaluate) gives the same wrong result, so the bug is in the bundled Javaluator parser and predates Evaluator.compile. ^ is left associative, which may be related to how the unary minus binds.

Expected: either the correct value or a parse/compile error. Never a silently wrong number.

Impact

Divinity's combat.yml lets admins write damage formulas. A plausible damage * 2^-1 would silently compute wrong damage.

Downstream workaround to remove once fixed

magemonkeystudio/divinity#387 added a guard: CombatFormulas rejects any override where ^ is followed (ignoring whitespace) by -, and tells the admin to write 2^(-1). After this is fixed, Divinity should:

  • remove the NEGATIVE_EXPONENT check in src/main/java/studio/magemonkey/divinity/manager/damage/formula/CombatFormulas.java;
  • remove its test CombatFormulasTest.brokenFormula_negativeExponentWithoutBrackets_isRejected (negativeExponentWithBrackets_isAccepted can stay);
  • drop the related wording in src/main/resources/combat.yml and doc/COMBAT_RESTRUCTURE_NOTES.md;
  • raise MIN_CORE_VERSION to the Codex build with the fix.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions