### Install math-expression-evaluator via npm Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Use npm to install the package for Node.js environments. ```bash npm install math-expression-evaluator ``` -------------------------------- ### Install math-expression-evaluator via bower Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Use bower to install the package for browser environments. ```bash bower install math-expression-evaluator ``` -------------------------------- ### Create Mexp Evaluator Instance Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Instantiate the Mexp class to initialize built-in tokens and math functions. The instance holds state for added tokens, persisting across subsequent eval calls. ```javascript import Mexp from 'math-expression-evaluator' // or: const Mexp = require('math-expression-evaluator') const mexp = new Mexp() // Basic usage — evaluate a string expression console.log(mexp.eval('2+3')) // 5 console.log(mexp.eval('10/4')) // 2.5 console.log(mexp.eval('2^8')) // 256 console.log(mexp.eval('3^2-2^2')) // 5 console.log(mexp.eval('2(7-4)3')) // 18 (implicit multiplication) console.log(mexp.eval('(-2)')) // -2 console.log(mexp.eval('2*-2')) // -4 console.log(mexp.eval('5*.8')) // 4 (leading zero optional) ``` -------------------------------- ### new Mexp() — Create an evaluator instance Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Instantiates the Mexp class, initializing all built-in tokens and math functions. The instance holds state for added tokens, so tokens registered via `addToken` persist across subsequent `eval` calls on the same instance. ```APIDOC ## new Mexp() ### Description Instantiates the `Mexp` class, initializing all built-in tokens and math functions. The instance holds state for added tokens, so tokens registered via `addToken` persist across subsequent `eval` calls on the same instance. ### Usage ```javascript import Mexp from 'math-expression-evaluator' // or: const Mexp = require('math-expression-evaluator') const mexp = new Mexp() // Basic usage — evaluate a string expression console.log(mexp.eval('2+3')) // 5 console.log(mexp.eval('10/4')) // 2.5 console.log(mexp.eval('2^8')) // 256 console.log(mexp.eval('3^2-2^2')) // 5 console.log(mexp.eval('2(7-4)3')) // 18 (implicit multiplication) console.log(mexp.eval('(-2)')) // -2 console.log(mexp.eval('2*-2')) // -4 console.log(mexp.eval('5*.8')) // 4 (leading zero optional) ``` ``` -------------------------------- ### Evaluate a math expression using Mexp.eval() Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Instantiate Mexp and use the eval method to compute the value of a given expression string. This is the most straightforward way to evaluate expressions. ```javascript const mexp = new Mexp() var value = mexp.eval(exp); // 2 + 2 ``` -------------------------------- ### mexp.postfixEval(postfixTokens, constants?) Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Evaluates a postfix token array using a stack-based algorithm. It accepts an optional `constants` map to inject runtime values for named constants. ```APIDOC ## `mexp.postfixEval(postfixTokens, constants?)` — Evaluate postfix tokens The third pipeline stage. Evaluates a postfix token array using a stack-based algorithm. Accepts an optional `constants` map to inject runtime values for named constants (used internally by `Sigma` and `Pi`). ```javascript const mexp = new Mexp() const lexed = mexp.lex('3^2-2^2') const postfix = mexp.toPostfix(lexed) const result = mexp.postfixEval(postfix) console.log(result) // 5 // Using custom constants at eval time const lexed2 = mexp.lex('x+y', [ { type: 3, token: 'x', show: 'x', value: 'x' }, { type: 3, token: 'y', show: 'y', value: 'y' }, ]) const postfix2 = mexp.toPostfix(lexed2) const result2 = mexp.postfixEval(postfix2, { x: 10, y: 7 }) console.log(result2) // 17 ``` ``` -------------------------------- ### Run tests for math-expression-evaluator Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Execute the test suite to verify the functionality and integrity of the evaluator. ```bash npm test ``` -------------------------------- ### Evaluate a math expression using Mexp constituents Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Break down expression evaluation into lexing, conversion to postfix, and postfix evaluation. This approach allows for intermediate inspection and manipulation of the expression's tokens. ```javascript const mexp = new Mexp var lexed = mexp.lex("expression"); var postfixed = mexp.toPostfix(lexed); var result = mexp.postfixEval(postfixed); console.log(result) ``` -------------------------------- ### Convert to Postfix Notation with mexp.toPostfix Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Applies the Shunting-Yard algorithm to reorder tokens into Reverse Polish Notation (postfix). This is useful for inspecting or serializing expressions before evaluation. ```javascript const mexp = new Mexp() const lexed = mexp.lex('2+3*4') const postfix = mexp.toPostfix(lexed) // Postfix order: 2 3 4 * + console.log(postfix.map(t => t.show)) // ['2', '3', '4', '×', '+'] // Useful for inspecting or serializing expressions before evaluation const lexed2 = mexp.lex('sin90+cos180') const postfix2 = mexp.toPostfix(lexed2) console.log(postfix2.map(t => t.show)) // ['90', 'sin', '180', 'cos', '+'] ``` -------------------------------- ### mexp.toPostfix(lexedTokens) Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Applies the Shunting-Yard algorithm to reorder tokens into Reverse Polish Notation (postfix), respecting operator precedence and associativity. This is the second pipeline stage. ```APIDOC ## `mexp.toPostfix(lexedTokens)` — Convert to postfix notation The second pipeline stage. Takes the output of `lex` and applies the Shunting-Yard algorithm to reorder tokens into Reverse Polish Notation (postfix), respecting operator precedence and associativity. ```javascript const mexp = new Mexp() const lexed = mexp.lex('2+3*4') const postfix = mexp.toPostfix(lexed) // Postfix order: 2 3 4 * + console.log(postfix.map(t => t.show)) // ['2', '3', '4', '×', '+'] // Useful for inspecting or serializing expressions before evaluation const lexed2 = mexp.lex('sin90+cos180') const postfix2 = mexp.toPostfix(lexed2) console.log(postfix2.map(t => t.show)) // ['90', 'sin', '180', 'cos', '+'] ``` ``` -------------------------------- ### Evaluate Postfix Tokens with mexp.postfixEval Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Evaluates a postfix token array using a stack-based algorithm. It accepts an optional constants map to inject runtime values for named constants. ```javascript const mexp = new Mexp() const lexed = mexp.lex('3^2-2^2') const postfix = mexp.toPostfix(lexed) const result = mexp.postfixEval(postfix) console.log(result) // 5 // Using custom constants at eval time const lexed2 = mexp.lex('x+y', [ { type: 3, token: 'x', show: 'x', value: 'x' }, { type: 3, token: 'y', show: 'y', value: 'y' }, ]) const postfix2 = mexp.toPostfix(lexed2) const result2 = mexp.postfixEval(postfix2, { x: 10, y: 7 }) console.log(result2) // 17 ``` -------------------------------- ### Evaluate Math Expressions with Mexp Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt The primary API for parsing and evaluating math expression strings. Optionally accepts custom tokens and constants. Throws descriptive Errors for invalid expressions. ```javascript const mexp = new Mexp() // --- Simple arithmetic --- mexp.eval('2+3-1') // 4 mexp.eval('2*5/10') // 1 mexp.eval('7 Mod 3') // 1 mexp.eval('3&1') // 1 (bitwise AND) // --- Trigonometric (degree mode by default) --- mexp.eval('sin90') // 1 mexp.eval('cos180') // -1 mexp.eval('tan45') // 1 mexp.eval('sin(cos(tan(90)))') // ~0.01726 // --- Inverse trig --- mexp.eval('asin1') // 90 (degrees) mexp.eval('acos1') // 0 mexp.eval('atan1') // 45 // --- Hyperbolic trig --- mexp.eval('sinh1') mexp.eval('cosh0') // 1 mexp.eval('tanh0') // 0 // --- Logarithms --- mexp.eval('log1000') // 3 (base-10) mexp.eval('ln' + Math.E) // ~1 (natural log) // --- Root & power --- mexp.eval('root4') // 2 mexp.eval('pow(2,3)') // 8 mexp.eval('2^10') // 1024 // --- Factorial, Permutation, Combination --- mexp.eval('5!') // 120 mexp.eval('5P3') // 60 mexp.eval('4C2') // 6 // --- Constants --- mexp.eval('pi') // 3.141592653589793 mexp.eval('e') // 2.718281828459045 mexp.eval('-e') // -2.718281828459045 // --- Parenthesis auto-close --- mexp.eval('((2+3*4') // 14 (missing closing parens auto-filled) mexp.eval('(2+(3-4') // 1 // --- Custom constant via third argument --- mexp.eval('mexp*3', [{ type: 3, show: 'mexp', token: 'mexp', value: 'mexp' }], { mexp: 5 } ) // 15 // --- Error handling --- try { mexp.eval('2*') } catch (e) { console.error(e.message) // "complete the expression" } try { mexp.eval('2**3') } catch (e) { console.error(e.message) // "* is not allowed after *" } try { mexp.eval('1 2') } catch (e) { console.error(e.message) // "Unexpected Space" } ``` -------------------------------- ### mexp.lex(expression, tokens?) Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Converts a raw expression string into an array of parsed token objects. It validates the syntax and throws an error on illegal token sequences. Custom tokens can be provided as an optional second argument. ```APIDOC ## `mexp.lex(expression, tokens?)` — Tokenize an expression The first pipeline stage. Converts a raw expression string into an array of parsed token objects with type, value, precedence, and display information. Validates syntax and throws on illegal token sequences. ```javascript const mexp = new Mexp() const tokens = mexp.lex('2+3') // Returns array of ParsedToken objects: // [ // { value: '(', type: 4, precedence: 0, show: '(' }, // auto-added opening paren // { value: 2, type: 1, precedence: 0, show: '2' }, // { value: fn, type: 9, precedence: 1, show: '+' }, // { value: 3, type: 1, precedence: 0, show: '3' }, // { value: ')', type: 5, precedence: 0, show: ')' } // auto-added closing paren // ] console.log(tokens.map(t => t.show)) // ['(', '2', '+', '3', ')'] // Lex with custom tokens injected inline const lexed = mexp.lex('myConst+1', [ { type: 3, token: 'myConst', show: 'K', value: 'K' } ]) // myConst is now recognized as a CONSTANT token ``` ``` -------------------------------- ### mexp.addToken(tokens) Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Registers custom tokens persistently with the Mexp instance. Once added, these tokens are available for all subsequent `eval`, `lex`, and `postfix` calls on that instance. Existing tokens with the same string identifier will be overwritten. ```APIDOC ## `mexp.addToken(tokens)` — Register custom tokens persistently Adds one or more custom token definitions to the `Mexp` instance. Once added, tokens are available in all subsequent `eval`, `lex`, and `postfix` calls on that instance. If a token with the same string already exists, it is overwritten. ```javascript const mexp = new Mexp() // Add a binary operator: nth-root (e.g. "27nroot3" → 3) mexp.addToken([ { type: 2, // BINARY_OPERATOR_HIGH_PRECEDENCE token: 'nroot', show: 'nroot', value: (a, b) => Math.pow(a, 1 / b), } ]) console.log(mexp.eval('27nroot3')) // 3 console.log(mexp.eval('8nroot3')) // 2 // Add a unary prefix function: absolute value mexp.addToken([ { type: 0, // FUNCTION_WITH_ONE_ARG token: 'positive', show: 'positive', value: (a) => Math.abs(a), } ]) console.log(mexp.eval('root(positive(2-6))')) // 2 (√|2-6| = √4 = 2) // Add an alias operator (e.g. "X" as multiplication) mexp.addToken([ { type: 2, token: 'X', show: 'X', value: mexp.math.mul, } ]) console.log(mexp.eval('2X3')) // 6 // Add a two-argument function: min mexp.addToken([ { type: 8, // FUNCTION_WITH_N_ARGS token: 'min', show: 'min', numberOfArguments: 2, value: (a, b) => Math.min(a, b), } ]) console.log(mexp.eval('min(10,4)')) // 4 ``` ``` -------------------------------- ### Add custom tokens using Mexp constituents Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Incorporate custom tokens when using the lex, toPostfix, and postfixEval methods. This allows for token extension at the lexing stage. ```javascript const mexp = new Mexp() const answer = mexp.postfixEval(mexp.toPostfix(mexp.lexed("expression", [token1, token2]))) console.log(answer) // tokens once added will be preserved in later evaluations ``` -------------------------------- ### Register Custom Tokens Persistently with mexp.addToken Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Adds custom token definitions to the Mexp instance, making them available for all subsequent operations. Existing tokens with the same string are overwritten. ```javascript const mexp = new Mexp() // Add a binary operator: nth-root (e.g. "27nroot3" → 3) mexp.addToken([ { type: 2, // BINARY_OPERATOR_HIGH_PRECEDENCE token: 'nroot', show: 'nroot', value: (a, b) => Math.pow(a, 1 / b), } ]) console.log(mexp.eval('27nroot3')) // 3 console.log(mexp.eval('8nroot3')) // 2 // Add a unary prefix function: absolute value mexp.addToken([ { type: 0, // FUNCTION_WITH_ONE_ARG token: 'positive', show: 'positive', value: (a) => Math.abs(a), } ]) console.log(mexp.eval('root(positive(2-6))')) // 2 (√|2-6| = √4 = 2) // Add an alias operator (e.g. "X" as multiplication) mexp.addToken([ { type: 2, token: 'X', show: 'X', value: mexp.math.mul, } ]) console.log(mexp.eval('2X3')) // 6 // Add a two-argument function: min mexp.addToken([ { type: 8, // FUNCTION_WITH_N_ARGS token: 'min', show: 'min', numberOfArguments: 2, value: (a, b) => Math.min(a, b), } ]) console.log(mexp.eval('min(10,4)')) // 4 ``` -------------------------------- ### Add custom tokens using Mexp.eval() Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Provide custom tokens directly to the eval method for a single evaluation. These tokens are not permanently added to the Mexp instance. ```javascript const mexp = new Mexp() mexp.eval("expression", [token1, token2]) // tokens once added will be preserved in later evaluations ``` -------------------------------- ### Add custom tokens using Mexp.addToken() Source: https://github.com/bugwheels94/math-expression-evaluator/blob/master/README.md Extend the evaluator's capabilities by adding custom tokens. Tokens added via addToken are preserved across subsequent evaluations. ```javascript const mexp = new Mexp() mexp.addToken([token1, token2]) // tokens once added will be preserved in later evaluations ``` -------------------------------- ### Evaluate Pi product over a range Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Use the 'Pi' token to calculate the product of an expression over an integer range. The variable 'n' iterates from the low to the high bound, inclusive. ```javascript const mexp = new Mexp() // Π n from 1 to 5 = 5! = 120 console.log(mexp.eval('Pi1,5,n')) // 120 // Π n from 1 to 15 = 15! = 1307674368000 console.log(mexp.eval('Pi1,15,n')) // 1307674368000 // Π n from 1 to 10 = 10! = 3628800 console.log(mexp.eval('Pi1,10,n')) // 3628800 ``` -------------------------------- ### mexp.eval(expression, tokens?, constants?) — Evaluate an expression Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt The primary API. Parses and evaluates a math expression string end-to-end. Optionally accepts an array of custom tokens and a constants map. Throws descriptive `Error` objects for invalid expressions. ```APIDOC ## mexp.eval(expression, tokens?, constants?) ### Description The primary API. Parses and evaluates a math expression string end-to-end. Optionally accepts an array of custom tokens and a constants map. Throws descriptive `Error` objects for invalid expressions. ### Parameters #### expression - **expression** (string) - Required - The mathematical expression string to evaluate. #### tokens - **tokens** (Array) - Optional - An array of custom tokens to extend the evaluator's capabilities. - Each token object should have properties like `type`, `show`, `token`, and `value`. #### constants - **constants** (object) - Optional - A map of custom constants to be used in the expression. ### Usage Examples ```javascript const mexp = new Mexp() // --- Simple arithmetic --- mexp.eval('2+3-1') // 4 mexp.eval('2*5/10') // 1 mexp.eval('7 Mod 3') // 1 mexp.eval('3&1') // 1 (bitwise AND) // --- Trigonometric (degree mode by default) --- mexp.eval('sin90') // 1 mexp.eval('cos180') // -1 mexp.eval('tan45') // 1 mexp.eval('sin(cos(tan(90)))') // ~0.01726 // --- Inverse trig --- mexp.eval('asin1') // 90 (degrees) mexp.eval('acos1') // 0 mexp.eval('atan1') // 45 // --- Hyperbolic trig --- mexp.eval('sinh1') mexp.eval('cosh0') // 1 mexp.eval('tanh0') // 0 // --- Logarithms --- mexp.eval('log1000') // 3 (base-10) mexp.eval('ln' + Math.E) // ~1 (natural log) // --- Root & power --- mexp.eval('root4') // 2 mexp.eval('pow(2,3)') // 8 mexp.eval('2^10') // 1024 // --- Factorial, Permutation, Combination --- mexp.eval('5!') // 120 mexp.eval('5P3') // 60 mexp.eval('4C2') // 6 // --- Constants --- mexp.eval('pi') // 3.141592653589793 mexp.eval('e') // 2.718281828459045 mexp.eval('-e') // -2.718281828459045 // --- Parenthesis auto-close --- mexp.eval('((2+3*4') // 14 (missing closing parens auto-filled) mexp.eval('(2+(3-4') // 1 // --- Custom constant via third argument --- mexp.eval('mexp*3', [{ type: 3, show: 'mexp', token: 'mexp', value: 'mexp' }], { mexp: 5 } ) // 15 // --- Error handling --- try { mexp.eval('2*') } catch (e) { console.error(e.message) // "complete the expression" } try { mexp.eval('2**3') } catch (e) { console.error(e.message) // "* is not allowed after *" } try { mexp.eval('1 2') } catch (e) { console.error(e.message) // "Unexpected Space" } ``` ``` -------------------------------- ### `Pi` — Product over a range Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Built-in token for multiplying an expression over an integer range. Syntax: `Pi(low, high, expr)` or `Pilow,high,expr`. The variable `n` iterates from `low` to `high` inclusive. ```APIDOC ## `Pi` — Product over a range Built-in token for multiplying an expression over an integer range. Syntax: `Pi(low, high, expr)` or `Pilow,high,expr`. The variable `n` iterates from `low` to `high` inclusive. ### Request Example ```javascript const mexp = new Mexp() // Π n from 1 to 5 = 5! = 120 console.log(mexp.eval('Pi1,5,n')) // 120 // Π n from 1 to 15 = 15! = 1307674368000 console.log(mexp.eval('Pi1,15,n')) // 1307674368000 // Π n from 1 to 10 = 10! = 3628800 console.log(mexp.eval('Pi1,10,n')) // 3628800 ``` ``` -------------------------------- ### Tokenize Expression with mexp.lex Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Converts a raw expression string into an array of parsed token objects. It validates syntax and can accept custom tokens inline. ```javascript const mexp = new Mexp() const tokens = mexp.lex('2+3') // Returns array of ParsedToken objects: // [ // { value: '(', type: 4, precedence: 0, show: '(' }, // auto-added opening paren // { value: 2, type: 1, precedence: 0, show: '2' }, // { value: fn, type: 9, precedence: 1, show: '+' }, // { value: 3, type: 1, precedence: 0, show: '3' }, // { value: ')', type: 5, precedence: 0, show: ')' } // auto-added closing paren // ] console.log(tokens.map(t => t.show)) // ['(', '2', '+', '3', ')'] // Lex with custom tokens injected inline const lexed = mexp.lex('myConst+1', [ { type: 3, token: 'myConst', show: 'K', value: 'K' } ]) // myConst is now recognized as a CONSTANT token ``` -------------------------------- ### `mexp.math.isDegree` — Toggle degree/radian mode Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Controls whether trigonometric functions (`sin`, `cos`, `tan`, `asin`, `acos`, `atan`) interpret their arguments in degrees (`true`, default) or radians (`false`). ```APIDOC ## `mexp.math.isDegree` — Toggle degree/radian mode Controls whether trigonometric functions (`sin`, `cos`, `tan`, `asin`, `acos`, `atan`) interpret their arguments in degrees (`true`, default) or radians (`false`). ### Request Example ```javascript const mexp = new Mexp() // Default: degree mode console.log(mexp.eval('tan45')) // 1 console.log(mexp.eval('sin90')) // 1 console.log(mexp.eval('cos180')) // -1 // Switch to radian mode mexp.math.isDegree = false console.log(mexp.eval('sin' + Math.PI)) // ~0 (sin(π) ≈ 0) console.log(mexp.eval('cos' + Math.PI)) // -1 console.log(mexp.eval('tan' + (Math.PI / 4))) // ~1 // Switch back to degree mode mexp.math.isDegree = true console.log(mexp.eval('sin90')) // 1 ``` ``` -------------------------------- ### Summation with Sigma Token Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Uses the built-in Sigma token for summing an expression over an integer range. The variable 'n' iterates from 'low' to 'high' inclusive. ```javascript const mexp = new Mexp() // Σ n from 1 to 100 = 5050 console.log(mexp.eval('Sigma1,100,n')) // 5050 // Σ n from 1 to 2 = 3 console.log(mexp.eval('Sigma1,2,n')) // 3 // Σ n² from 1 to 2 = 1 + 4 = 5 console.log(mexp.eval('Sigma1,2,(n*n)')) // 5 // Sigma with a custom constant inside console.log( mexp.eval( 'Sigma1,3,2', [{ type: 3, value: 'x', show: 'x', token: 'x' }], { x: 2 } ) ) // 6 (2+2+2) ``` -------------------------------- ### Sigma Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Built-in token for summation over a range. It supports the syntax `Sigma(low, high, expr)` or `Sigmalow,high,expr`, where the variable `n` iterates from `low` to `high` inclusive. ```APIDOC ## `Sigma` — Summation over a range Built-in token for summing an expression over an integer range. Syntax: `Sigma(low, high, expr)` or `Sigmalow,high,expr`. The variable `n` iterates from `low` to `high` inclusive. ```javascript const mexp = new Mexp() // Σ n from 1 to 100 = 5050 console.log(mexp.eval('Sigma1,100,n')) // 5050 // Σ n from 1 to 2 = 3 console.log(mexp.eval('Sigma1,2,n')) // 3 // Σ n² from 1 to 2 = 1 + 4 = 5 console.log(mexp.eval('Sigma1,2,(n*n)')) // 5 // Sigma with a custom constant inside console.log( mexp.eval( 'Sigma1,3,2', [{ type: 3, value: 'x', show: 'x', token: 'x' }], { x: 2 } ) ) // 6 (2+2+2) ``` ``` -------------------------------- ### Toggle degree/radian mode for trigonometric functions Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Control whether trigonometric functions interpret arguments in degrees or radians by setting the `mexp.math.isDegree` property. The default is degree mode (`true`). ```javascript const mexp = new Mexp() // Default: degree mode console.log(mexp.eval('tan45')) // 1 console.log(mexp.eval('sin90')) // 1 console.log(mexp.eval('cos180')) // -1 // Switch to radian mode mexp.math.isDegree = false console.log(mexp.eval('sin' + Math.PI)) // ~0 (sin(π) ≈ 0) console.log(mexp.eval('cos' + Math.PI)) // -1 console.log(mexp.eval('tan' + (Math.PI / 4))) // ~1 // Switch back to degree mode mexp.math.isDegree = true console.log(mexp.eval('sin90')) // 1 ``` -------------------------------- ### Access token type enum Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt Use the static `Mexp.tokenTypes` enum to map token category names to numeric identifiers when defining custom tokens. This avoids hardcoding magic numbers. ```javascript import Mexp from 'math-expression-evaluator' console.log(Mexp.tokenTypes) // { // FUNCTION_WITH_ONE_ARG: 0, // NUMBER: 1, // BINARY_OPERATOR_HIGH_PRECENDENCE: 2, // CONSTANT: 3, // OPENING_PARENTHESIS: 4, // CLOSING_PARENTHESIS: 5, // DECIMAL: 6, // POSTFIX_FUNCTION_WITH_ONE_ARG: 7, // FUNCTION_WITH_N_ARGS: 8, // BINARY_OPERATOR_LOW_PRECENDENCE: 9, // BINARY_OPERATOR_PERMUTATION: 10, // COMMA: 11, // EVALUATED_FUNCTION: 12, // EVALUATED_FUNCTION_PARAMETER: 13, // SPACE: 14 // } // Use in token definitions for readability const mexp = new Mexp() mexp.addToken([ { type: Mexp.tokenTypes.FUNCTION_WITH_N_ARGS, token: 'maxof5', show: 'maxof5', numberOfArguments: 5, value: (a, b, c, d, e) => Math.max(a, b, c, d, e), } ]) console.log(mexp.eval('maxof5(7,12,23,33,2)')) // 33 ``` -------------------------------- ### `Mexp.tokenTypes` — Token type enum Source: https://context7.com/bugwheels94/math-expression-evaluator/llms.txt A static enum that maps token category names to their numeric identifiers. Use it when defining custom tokens to avoid hardcoding magic numbers. ```APIDOC ## `Mexp.tokenTypes` — Token type enum A static enum that maps token category names to their numeric identifiers. Use it when defining custom tokens to avoid hardcoding magic numbers. ### Request Example ```javascript import Mexp from 'math-expression-evaluator' console.log(Mexp.tokenTypes) // { // FUNCTION_WITH_ONE_ARG: 0, // NUMBER: 1, // BINARY_OPERATOR_HIGH_PRECENDENCE: 2, // CONSTANT: 3, // OPENING_PARENTHESIS: 4, // CLOSING_PARENTHESIS: 5, // DECIMAL: 6, // POSTFIX_FUNCTION_WITH_ONE_ARG: 7, // FUNCTION_WITH_N_ARGS: 8, // BINARY_OPERATOR_LOW_PRECENDENCE: 9, // BINARY_OPERATOR_PERMUTATION: 10, // COMMA: 11, // EVALUATED_FUNCTION: 12, // EVALUATED_FUNCTION_PARAMETER: 13, // SPACE: 14 // } // Use in token definitions for readability const mexp = new Mexp() mexp.addToken([ { type: Mexp.tokenTypes.FUNCTION_WITH_N_ARGS, token: 'maxof5', show: 'maxof5', numberOfArguments: 5, value: (a, b, c, d, e) => Math.max(a, b, c, d, e), } ]) console.log(mexp.eval('maxof5(7,12,23,33,2)')) // 33 ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.