Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic Functions work even when not loaded #3200

Open
maorfaingold opened this issue May 5, 2024 · 5 comments
Open

Arithmetic Functions work even when not loaded #3200

maorfaingold opened this issue May 5, 2024 · 5 comments
Labels

Comments

@maorfaingold
Copy link

I'm using custom evaluate with only evaluateDependencies but still some of the arithmetic functions are working:

const { create, evaluateDependencies } = require('mathjs')

const { evaluate } = create({
  evaluateDependencies
})

console.log(evaluate('2 - 1')) // correctly fails because subtract is not defined
console.log(evaluate('3 / 3')) // correctly fails because divide is not defined

console.log(evaluate('2 + 3')) // succeeds even though add is not defined
console.log(evaluate('2 * 3')) // succeeds even though multiply is not defined
console.log(evaluate('2 ^ 3')) // succeeds even though pow is not defined

(there may be more arithmetic functions that are working I didn't checked them all)

there is a similar issue: #2876
but there the issue was with 'det' and the suggested solution was to use
require('mathjs/number')

I'm not sure this solution will solve my issue but can't know for sure because mathjs/number missing typescript definitions as described in this open issue:
#2506

@josdejong
Copy link
Owner

Thanks for your input.

Missing TypeScript definitions for 'mathjs/number' is discussed in #2506

Can you explain what you try to achieve? If you want to disable specific operators, you could do something like the following:

  • Parse the expression via math.parse
  • Using .filter(), check if the parsed expression contains OperatorNodes that have a .op property that you want to disable.
  • If those are found, throw an error like "Operator +" not supported.

See docs about parse and filter: https://mathjs.org/docs/expressions/expression_trees.html

@maorfaingold
Copy link
Author

@josdejong
In my specific case I want a textbox that allows only numbers, if the user's input contains any arithmetic operators I want it to throw an error.

In my case I use:

const { create, evaluateDependencies, unaryMinusDependencies } = require('mathjs')

const { evaluate } = create({
  evaluateDependencies,
  unaryMinusDependencies  // to allow negative numbers (not a must - also part of the described issue)
})

to create a custom evaluate that should throw an error if the user's input includes any operators.

Sure there are workarounds for the issue, mine looks like this:


const { create, factory, evaluateDependencies, unaryMinusDependencies } = require('mathjs')

const { evaluate } = create({
  evaluateDependencies,
  unaryMinusDependencies,  // to allow negative numbers (not a must - also part of the described issue)
  createAdd: factory('add', [], () => () => { throw new Error('Error: Function add missing in provided namespace "math"'); } ),
  createMultiply: factory('multiply', [], () => () => { throw new Error('Error: Function multiply missing in provided namespace "math"'); } ),
 createPow: factory('pow', [], () => () => { throw new Error('Error: Function pow missing in provided namespace "math"'); } )
})

this way I can mimic the expected behavior (the same as with substract and divide).

The issue is not critical and have pretty simple workarounds but it's an issue in the code - the expected behavior is if I create a custom evaluate using only 'evaluateDependencies' all the arithmetic operators should not be imported (surely not partially imported - in this case 'subtract' and 'divide' throw errors and the rest not - 'add', 'multiply', 'pow', and also unaryMinus works when not using 'unaryMinusDependencies'.

@josdejong
Copy link
Owner

In my specific case I want a textbox that allows only numbers

That sounds like you do not need mathjs at all? If the input is just a number you can use JavaScript's parseFloat or parseInt.

@maorfaingold
Copy link
Author

I oversimplified my use case - I need to support any possible combination of arithmetic functions. One of the cases is not supporting any functions at all as I described above, other cases the user may want only add and subtract, or maybe only multiple and divide, or all functions except log etc.

@josdejong
Copy link
Owner

In that case I think the best way is to parse the expression into an expression tree and validate this according to the limitations you want to apply, like I explained in #3200 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants