zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
459 stars 86 forks source link

Unable to cast negative number as a decimal #154

Closed JamesHill3 closed 5 months ago

JamesHill3 commented 11 months ago

We use the this framework to evaluate user-created formulas where values are replaced at runtime. I offer that explanation only to justify the hideous C# I'm about to post.

The user-created expression/formula below is not able to be evaluated by the Eval-Expression framework:

Math.Round((((decimal)-2 / 2) - (decimal)0.0625 - ((decimal)2 / 2)) * 32) / 32

The error is:

Operator '-' cannot be applied to operands of type 'RuntimeType' and 'int'. The error occurred for expression "-" at position 22 near "-2 / 2) - (decimal)0.0625".

This same expression is evaluated successfully in dotnetfiddle.net.

Removing the '-' from the (decimal)-2 allows the evaluation framework to work.

UPDATE/EDIT:

I'd like to add that after further testing, the bug can be more easily reproduced: (decimal)-2.

JonathanMagnan commented 11 months ago

Hello @JamesHill3 ,

Thank you for reporting it. I can confirm that we can easily reproduce this bug and we will look at it to make a fix.

Best Regards,

Jon

JonathanMagnan commented 11 months ago

Hello @JamesHill3 ,

The v6.0.4 has been released today.

We don't feel the fix is perfect yet, so we will discuss how we can further improve it. So far, we have found that is still possible to get some issues when using a variable or navigation path (instead of a type to cast) in parenthesis before the subtract sign.

Here is a working example: https://dotnetfiddle.net/NtRRYg

Best Regards,

Jon

JonathanMagnan commented 5 months ago

Hello @JamesHill3 ,

A new version has been released today, improving our fixes even more so that they support more cases.

If you upgrade to the v6.1.7 or later, make sure to double-check if everything works correctly.

I will close this issue now, as I believe we completed it.

Best Regards,

Jon