-
Notifications
You must be signed in to change notification settings - Fork 8
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
Bad multiply-division parsing #6
Comments
@sytabaresa thanks for bringing my attention to this, I'll see if I can implement a pull request for this. In the meantime, I don't know if you're looking for a work-around but I just tested a gist I wrote a while back, and the problem does not seem to exist there so I'll share it here in case it helps you. To get your function, you can compile the // import compiler defined in compiler.js
const { compile } = require('./compiler')
// use this mapping for named parameters
const map = (a, b, c) => ({ a, b, c })
// compile these expressions
const f = compile('1/a*b*c', map)
const g = compile('(1/a)*b*c', map)
console.log(f(2, 3, 4))
console.log(g(2, 3, 4)) They both output P.S. If you want the functions to accept instances of the complex class instead, you'll need to modify the operators in const Complex = require('complex-js');
// initialize operator expressions
const [
add, subtract,
multiply, divide,
mod, power,
negate, invoke
] = [
(a, b) => a.add(b), (a, b) => a.subtract(b),
(a, b) => a.multiply(b), (a, b) => a.divide(b),
(a, b) => a.modulo(b), (a, b) => a.power(b),
(a) => Complex.negate(a), (fn, ...a) => fn(...a)
].map(operation) I'll update this issue once I implement the fix. |
@sytabaresa Hey, I've got a new version of the library in progress, but so far things look pretty promising! This order of operations bug has been fixed in the new compiler which uses nearley for parsing and moo for lexing, and the updated call signature looks like this: Complex.compile('1/a*b*c', (a, b, c) => ({ a, b, c }))
Let me know what you think and if this is sufficient for your purposes. Edit It's on npm now so you should just be able to run:
|
Hi, nice library. lightweight and so useful.
But trying to make a app, I note that some expressions are not parsed correctly:
returns a optimized function in the browser:
and
returns
that are different expressions.
*
and/
operators should be the same precedence and the correct result is the last (a chain of operators without grouping).Thanks
complex-js version: 5.0.0
The text was updated successfully, but these errors were encountered: