zzzprojects / Eval-Expression.NET

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

Operator '^' cannot be applied to operands of type 'double' and 'double' #83

Closed jwr456 closed 4 years ago

jwr456 commented 4 years ago

I am using the following:

        var context = new EvalContext() { UseCache = false };
        context.UseCaretForExponent = true;

and getting the following exception

Operator '^' cannot be applied to operands of type 'double' and 'double'

when I try to Eval this

Eval.Execute("someOtherDouble*(1.5^2.0)", this);

and this Operator '^' cannot be applied to operands of type 'double' and 'int'

when I try to Eval this

Eval.Execute("someOtherDouble*(1.5^2)", this);

Can the Eval-Expression.NET not do exponents on doubles or am I doing something incorrectly?

Thanks,

Joel

jwr456 commented 4 years ago

Never mind, I figured this out:

Eval.Execute("someOtherDouble*Math.Pow(1.5,2)", this);

works

JonathanMagnan commented 4 years ago

Hello @jwr456 ,

I will re-open it as I believe we have an issue here and your initial code should have worked.

Best Regards,

Jon

JonathanMagnan commented 4 years ago

In fact, that's currently working.

The issue is that you create an instance context and specify options to it but you don't use it. You use instead the global context which doesn't have the option and which the ^ operator behaves like the one in C#

Instance Context

var context = new EvalContext() { UseCache = false };
context.UseCaretForExponent = true;
var x1 = context.Execute("3.1*(1.5^2.0)", this);

Global Context

EvalManager.DefaultContext.UseCaretForExponent = true;
var x1 = Eval.Execute("3.1*(1.5^2.0)", this);