-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
math.eval() doesn't parse the scope correctly #973
Comments
I think I understand what you mean but can you give an example of what is going wrong? |
For example, I set a variable in the scope object to sqrt(9), and it returns an error saying that it can't do anything with it. |
Can you post an code example? I guess you do something like: math.eval('a + 4', {a: 'sqrt(9)'})
// Uncaught Error: Cannot convert "sqrt(9)" to a number which will not work since the variables in the scope are not evaluated like as an expression. You can do something like: math.eval([
'a = sqrt(9)',
'a + 4'
])
// [3, 7] |
Yeah, basically that. The variables in the scope aren't evaluated, which is slightly confusing; in normal math you'd mentally parse |
In math.js, strings are also valid values, for example To achieve the behavior you want, you could evaluate the expressions you want to have in your scope Just for reference, we discussed something similar here: #907 (comment) |
I would recommend using eval of an (ordered) array of expressions in your original example because it's explicit and reliable. JavaScript object keys are not guaranteed to be enumerated in any particular order and do vary between engines / versions, modern engines enumeration order seems to be based on property definition order, but I remember a time when it was more common to use alphanumeric order. Which highlights another requirement if considering implementing this feature for scope: an expression parse followed by a DAG topological sort of the expressions references would be required to determine a valid evaluation order. |
If you put an equation through eval with variables and provide a scope with something other than just numbers as a value, it doesn't parse it correctly. I could get around this by just parsing it before putting it into the scope object, but this may not be possible for everyone.
The text was updated successfully, but these errors were encountered: