[Solved] Operator ‘+’ can’t be applied to operands of types ‘decimal’ and ‘double’ – NCalc [closed]


Ok. I looked at the source code. And here is what I found.

The Abs(-1) part of the expression is always evaluated as a decimal

Result = Math.Abs(Convert.ToDecimal(
                        Evaluate(function.Expressions[0]))
                        );

Cos(2) is evaluated as double

Result = Math.Cos(Convert.ToDouble(Evaluate(function.Expressions[0])));

And C# does not allow you to add these two types together.

The reason that Math.Abs(-1) + Math.Cos(2) works is that Math.Abs(-1) actually evaluates as int. And you can perfectly add an int to double.

You can not compile this piece for example (note m for decimal). Math.Abs(-1m) + Math.Cos(2);
which is actually what NCalc is trying to do when you type out

new Expression("Abs(-1) + Cos(2)").Evaluate()

I would call this a bug. You can try to edit the source and try to fix this problem or find some other option.

3

solved Operator ‘+’ can’t be applied to operands of types ‘decimal’ and ‘double’ – NCalc [closed]