Eval-Expression.NET icon indicating copy to clipboard operation
Eval-Expression.NET copied to clipboard

Unable to cast negative number as a decimal

Open J3-code opened this issue 2 years ago • 2 comments

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.

J3-code avatar Nov 28 '23 13:11 J3-code

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 avatar Nov 28 '23 15:11 JonathanMagnan

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 avatar Dec 11 '23 22:12 JonathanMagnan

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

JonathanMagnan avatar Jun 18 '24 21:06 JonathanMagnan