Operator '+' can’t be Applied to operands of types 'decimal' and 'double' - Ncalc

Asked

Viewed 172 times

0

I’m using the Ncalc lib

A simple formula such as "Abs(-1) + Cos(2)" gives the following exception:

Operator '+' can’t be Applied to operands of types 'decimal' and 'double'

Why? How to solve?

The calculation is to be executed as follows:

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

The only related discussion on the project website is a bit old and talks about editing the source code https://ncalc.codeplex.com/discussions/346702

  • Your answer is not constructive. You just translated the exception text. My question is that Math.Abs(-1)+Math.Cos(2) works in C# but not in the Ncalc library. If you’ve never used or know the library or should be commenting here.

  • OK, no problem.

  • 1

    @Omni and all. Please do not throw suggestions into the air. If you have never used NCALC you will not understand my question. 1º I cannot pass -1m and 2m because they are not valid numbers. 2º It is not the Abs and Cos parameters that have the problem. It is the sum of the result of Abs and Cos

  • One of the functions, Abs or Cos, is returning a value decimal and another is returning one double. I believe that if you have access to the lib source it would be better to change the return type of one of the functions to avoid the error or use some type conversion feature if it has.

  • @Richarddias, lib is open source. I have already asked the question on codeplex I don’t have enough knowledge to try to figure out if it’s a code problem or if I’m doing something wrong on my side.

  • I believe it is lib or a misuse of it on your part. You pass a string for the class constructor Expression and this string is evaluated and transformed into a correct formula?

  • I think you already have the answer in your question: https://ncalc.codeplex.com/discussions/346702 Now put your hands in the dough and change the source code to fix this problem

  • I don’t understand NCALC, but its expression is in double quotes, so I understand that it would be a concatenation and not a sum, not an arithmetic expression.

Show 3 more comments

3 answers

2

There is no problem with the lib Ncalc, the question is that one method returns Decimal and the other returns Double. The compiler . NET does not allow operations between these two object types due to the fact that the accuracy of the two are very different.

The correct is to convert the result of one of the methods to the data type of the other method.

using System;

public class Program
{
    public static void Main()
    {
        double dbl = 1;
        decimal dec = 2;

        // Neste exemplo converti o valor double para decimal.
        var result = Convert.ToDecimal(dbl) + dec;

        Console.WriteLine(result);

        // Esta operação retorna a exceção Operator '+' can'tbe applied to operands of types 'decimal' and 'double'
        Console.WriteLine(dbl + dec);
    }
}
  • 1

    There is a problem with lib. It is fixed in the source code but not in the latest version of binary http://ncalc.codeplex.com/SourceControl/changeset/5a719518101d

  • In reality what is missing is to leave a definitive build with the correction, more of the form that was proposed resolves temporarily until the release of the binary files.

1


0

using System;

class Program
{
  public void Main()
  {
    double a  = 1;
    decimal b = 2;
    a = Convert.ToDouble(1);
    b = Convert.ToDouble(2);

    int valor = a + b;

    Console.Write(int.ToString());

    return 0;
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.