Skip to content

Commit

Permalink
Fix precedence of percentage calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremypw committed Dec 2, 2023
1 parent 6c09010 commit 395a6e2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Core/Evaluation.vala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ namespace PantheonCalculator.Core {
Operator () { symbol = "÷", inputs = 2, prec = 2, fixity = "LEFT", eval = (a, b) => a / b },
Operator () { symbol = "mod", inputs = 2, prec = 2, fixity = "LEFT", eval = (a, b) => a % b },
Operator () { symbol = "^", inputs = 2, prec = 3, fixity = "RIGHT", eval = (a, b) => Math.pow (a, b) },
Operator () { symbol = "E", inputs = 2, prec = 4, fixity = "RIGHT", eval = (a, b) => a * Math.pow (10, b) }
Operator () { symbol = "E", inputs = 2, prec = 4, fixity = "RIGHT", eval = (a, b) => a * Math.pow (10, b) },
//Internal use only
//Hgh precedence multiply and divide for percentage evaluation
//Percentage always evaluates first as if it has parens around it
Operator () { symbol = "<*>", inputs = 2, prec = 7, fixity = "LEFT", eval = (a, b) => a * b },
Operator () { symbol = "<÷>", inputs = 2, prec = 6, fixity = "LEFT", eval = (a, b) => a / b }
};

private struct Function { string symbol; int inputs; Eval eval;}
Expand Down
4 changes: 2 additions & 2 deletions src/Core/Scanner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ namespace PantheonCalculator.Core {
(last_token.token_type == TokenType.P_LEFT))) {
throw new SCANNER_ERROR.INVALID_PERCENT (_("'%' must follow a value."));
} else {
token_list.append (new Token ("*", TokenType.OPERATOR));
token_list.append (new Token ("<*>", TokenType.OPERATOR));
token_list.append (new Token ("<CLV>", TokenType.CURRENT_LEFT_VALUE));
token_list.append (new Token ("/", TokenType.OPERATOR));
token_list.append (new Token ("<÷>", TokenType.OPERATOR));
last_token = new Token ("100", TokenType.NUMBER);
token_list.append (last_token);
continue;
Expand Down

0 comments on commit 395a6e2

Please sign in to comment.