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

Parameter name does not support operators #301

Closed
dzp0839 opened this issue Jan 15, 2024 · 2 comments
Closed

Parameter name does not support operators #301

dzp0839 opened this issue Jan 15, 2024 · 2 comments
Labels

Comments

@dzp0839
Copy link

dzp0839 commented Jan 15, 2024

Is there any way to handle situations where parameter names contain operators?

var interpreter = new Interpreter();
var parameters = new[]
{
    new Parameter("name-y", 24),
    new Parameter("y", 10)
};
var result = interpreter.Eval<double>("name-y / (y + 2)", parameters);

This code cannot obtain the correct result.

@davideicardi
Copy link
Member

I think this is not supported and it is out of scope for the project. We try to follow the same rules of C#, where name-y is not a valid identifier.
Maybe you can preprocess the expression and replace the invalid identifier with something valid? Like name-y to name_y?

@dzp0839
Copy link
Author

dzp0839 commented Jan 18, 2024

Thank you for your suggestion. Actually, due to my usage scenario, I need to perform calculations based on parameters, like this

  var interpreter = new Interpreter();
  var formula = "P1-X * (P2-Y + P3-Z)";
  var parameters = new[]
  {
      new Parameter("P1-X", 24),
      new Parameter("P2-Y", 10),
      new Parameter("P3-Z", 10)
  };
  var result = interpreter.Eval<double>(formula, parameters);

In the end, I chose to wrap my parameters in {} and replace them with the corresponding data to implement it

var interpreter = new Interpreter();
var formula = "{P1-X} * ({P2-Y} + {P3-Z})"; 
var result = interpreter.Eval<double>(ConvertHelper(formula)); //"24 * (10 + 10)"

@dzp0839 dzp0839 closed this as completed May 31, 2024
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