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

OptionForceIntegerNumbersEvaluationsAsDoubleByDefault leads to exception when dividing #110

Closed
midgleyc opened this issue Jul 29, 2021 · 2 comments
Assignees
Labels

Comments

@midgleyc
Copy link

midgleyc commented Jul 29, 2021

The code

var evaluator = new ExpressionEvaluator()
{
    OptionForceIntegerNumbersEvaluationsAsDoubleByDefault = true 
};

string expression = "3/Math.Round(Avg(1,2),MidpointRounding.AwayFromZero)";

Console.WriteLine(expression);
Console.WriteLine(evaluator.Evaluate(expression));

gives the error

Unhandled exception. Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Operator '/' cannot be applied to operands of type 'double' and 'decimal'

It worked in 1.4.20 and first stopped working in 1.4.21. Math.Round is picking the decimal overload when I'd expect it to pick the double overload. It works without OptionForceIntegerNumbersEvaluationsAsDoubleByDefault.

@codingseb
Copy link
Owner

Hello @midgleyc, thanks to reporting this bug.
I just have an other behavior on this one.
I do not have the Microsoft.CSharp.RuntimeBinder.RuntimeBinderException but it returns 2 instead.
I tested it with version 1.4.20 and 1.4.30

Anyway it should returns 1.5 so I will investigate why it return 2

@codingseb codingseb self-assigned this Aug 3, 2021
@codingseb codingseb added the bug label Aug 3, 2021
codingseb added a commit that referenced this issue Aug 3, 2021
Correction for #110 and better selection of methods
@codingseb
Copy link
Owner

Ok after some investigations I understood that it was a bug in method override selection.
And depending on which version of .net you use it selected an other method it depends on the order it received the potential methods list. So in your case I think it took Math.Round(decimal, MidpointRounding) and in my case Math.Round(double, int32) and automatically converted the enum in it's corresponding int value.
I made a correction and published version 1.4.31.0

So thanks again to finding out this bug

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