Add ** as an option for x^y

This commit is contained in:
Ean Milligan 2025-06-28 05:43:10 -04:00
parent c9772d3ccf
commit f0ea31edf3
2 changed files with 9 additions and 4 deletions

View File

@ -17,7 +17,7 @@ import { getMatchingParenIdx } from 'artigen/utils/parenBalance.ts';
// mathSolver is a function that recursively solves the full roll and math // mathSolver is a function that recursively solves the full roll and math
export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep => { export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep => {
// Initialize PEMDAS // Initialize PEMDAS
const signs = ['^', '*', '/', '%', '+', '-']; const signs = ['^', '**', '*', '/', '%', '+', '-'];
const stepSolve: SolvedStep = { const stepSolve: SolvedStep = {
total: 0, total: 0,
details: '', details: '',
@ -88,7 +88,11 @@ export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep =>
// At this point, conf should be [num, op, num, op, num, op, num, etc] // At this point, conf should be [num, op, num, op, num, op, num, etc]
// Evaluate all EMDAS by looping thru each tier of operators (exponential is the highest tier, addition/subtraction the lowest) // Evaluate all EMDAS by looping thru each tier of operators (exponential is the highest tier, addition/subtraction the lowest)
const allCurOps = [['^'], ['*', '/', '%'], ['+', '-']]; const allCurOps = [
['^', '**'],
['*', '/', '%'],
['+', '-'],
];
allCurOps.forEach((curOps) => { allCurOps.forEach((curOps) => {
loggingEnabled && log(LT.LOG, `Evaluating roll ${JSON.stringify(conf)} | Evaluating ${JSON.stringify(curOps)}`); loggingEnabled && log(LT.LOG, `Evaluating roll ${JSON.stringify(conf)} | Evaluating ${JSON.stringify(curOps)}`);
// Iterate thru all operators/operands in the conf // Iterate thru all operators/operands in the conf
@ -150,6 +154,7 @@ export const mathSolver = (conf: MathConf[], wrapDetails = false): SolvedStep =>
// Finally do the operator on the operands, throw an error if the operator is not found // Finally do the operator on the operands, throw an error if the operator is not found
switch (conf[i]) { switch (conf[i]) {
case '^': case '^':
case '**':
subStepSolve.total = Math.pow(oper1, oper2); subStepSolve.total = Math.pow(oper1, oper2);
break; break;
case '*': case '*':

View File

@ -17,7 +17,7 @@ import { loggingEnabled } from 'artigen/utils/logFlag.ts';
import { assertParenBalance } from 'artigen/utils/parenBalance.ts'; import { assertParenBalance } from 'artigen/utils/parenBalance.ts';
// minusOps are operators that will cause a negative sign to collapse into a number (in cases like + - 1) // minusOps are operators that will cause a negative sign to collapse into a number (in cases like + - 1)
const minusOps = ['(', '^', '*', '/', '%', '+', '-']; const minusOps = ['(', '^', '**', '*', '/', '%', '+', '-'];
const allOps = [...minusOps, ')']; const allOps = [...minusOps, ')'];
export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResults: number[]): [ReturnData[], CountDetails[], RollDistributionMap[]] => { export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResults: number[]): [ReturnData[], CountDetails[], RollDistributionMap[]] => {
@ -31,7 +31,7 @@ export const tokenizeMath = (cmd: string, modifiers: RollModifiers, previousResu
.replace(cmdSplitRegex, '') .replace(cmdSplitRegex, '')
.replace(internalWrapRegex, '') .replace(internalWrapRegex, '')
.replace(/ /g, '') .replace(/ /g, '')
.split(/([-+()*/^]|(?<![d%])%)|(x\d+(\.\d*)?)/g) .split(/(\*\*)|([-+()*/^]|(?<![d%])%)|(x\d+(\.\d*)?)/g)
.filter((x) => x); .filter((x) => x);
loggingEnabled && log(LT.LOG, `Split roll into mathConf ${JSON.stringify(mathConf)}`); loggingEnabled && log(LT.LOG, `Split roll into mathConf ${JSON.stringify(mathConf)}`);