Add support for more Math notations

This commit is contained in:
yuanchuan 2021-10-25 09:27:52 +08:00
parent 28d2bd6fde
commit 75a86c4925
1 changed files with 20 additions and 11 deletions

View File

@ -20,15 +20,18 @@ export default function(input, context) {
}
const operator = {
'^': 5,
'*': 4, '/': 4, '%': 4,
'+': 3, '-': 3,
'(': 2, ')': 2,
'<': 1, '>': 1,
'=': 1, '==': 1,
'≤': 1, '<=': 1,
'≥': 1, '>=': 1,
'≠': 1, '!=': 1,
'^': 7,
'*': 6, '/': 6, '÷': 6, '%': 6,
'&': 5, '|': 5,
'+': 4, '-': 4,
'<': 3, '>': 3,
'=': 3, '==': 3,
'≤': 3, '<=': 3,
'≥': 3, '>=': 3,
'≠': 3, '!=': 3,
'∧': 2, '&&': 2,
'': 2, '||': 2,
'(': 1 , ')': 1,
}
function calc(expr, context, repeat = []) {
@ -107,6 +110,9 @@ function get_tokens(input) {
if (c == '=' && last_token && /^[!<>=]$/.test(last_token.value)) {
last_token.value += c;
}
else if (/^[|&]$/.test(c) && last_token && last_token.value == c) {
last_token.value += c;
}
else if (c == '-' && expr[i - 1] == 'e') {
num += c;
}
@ -236,15 +242,18 @@ function compute(op, a, b) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '%': return a % b;
case '^': return Math.pow(a, b);
case '|': return a | b;
case '<': return a < b;
case '>': return a > b;
case '^': return Math.pow(a, b);
case '÷': case '/': return a / b;
case '=': case '==': return a == b;
case '≤': case '<=': return a <= b;
case '≥': case '>=': return a >= b;
case '≠': case '!=': return a != b;
case '∧': case '&&': return a && b;
case '': case '||': return a || b;
}
}