1

I want to calculate a string in C# using either NCalc or DynamicExpresso library, the problem is, when the calculation gets complex and the numbers are big, it returns the wrong result. For example the code below returns -808182895 when it should return 3486784401

string value = "387420489*9";
value = new Interpreter().Eval(value).ToString();

Am i doing anything wrong? Thanks for the help.

joseph
  • 37
  • 7
  • 2
    Does this answer your question? [how to convert a string to a mathematical expression programmatically](https://stackoverflow.com/questions/21750824/how-to-convert-a-string-to-a-mathematical-expression-programmatically) – Duck Ling Apr 06 '20 at 18:35
  • Are you aware of how integer overflow happens? – Joe Sewell Apr 06 '20 at 18:36
  • @PawelFlajszer not really, since the user enters the numbers and the operations, i just provided a fixed expression so it can be easier to understand, it would be better to use something like NCalc or an external library, for me at least – joseph Apr 06 '20 at 18:49
  • @JoeSewell yes i am aware of that, hence why i asked for help – joseph Apr 06 '20 at 18:50
  • 1
    (long)387420489*(long)9 works. – Oguz Ozgul Apr 06 '20 at 19:20
  • Tested here: http://dynamic-expresso.azurewebsites.net/ – Oguz Ozgul Apr 06 '20 at 19:20
  • If the string is parsed like C#, then `"387420489L*9L"` should ensure 64-bit signed integers, and `"387420489u*9u"` should ensure 32-bit _unsigned_ integers. – Jeppe Stig Nielsen Apr 06 '20 at 20:42
  • 1
    As other suggested I think that you must ensure that the variables are considered `long` or `decimal`. By default `9` is considered an integer and this will generate an overflow. You can write `387420489*(long)9` for example. – Davide Icardi Apr 07 '20 at 07:46
  • thanks @OguzOzgul your answer resolved my issue. – joseph Apr 07 '20 at 12:12
  • Thanks @DavideIcardi for the helpful answer – joseph Apr 07 '20 at 12:13
  • Can you mark it as solution if I add it as answer? comments are not answers actually. – Oguz Ozgul Apr 07 '20 at 12:20
  • Dear @phuclv the OP is having a specific problem with a third party component. He is not asking how to execute code statements dynamically – Oguz Ozgul Apr 07 '20 at 19:50

1 Answers1

1

Try the following:

(long)387420489 * (long)9

Dynamic Expresso has a web shell here where you can test the expressions;

http://dynamic-expresso.azurewebsites.net/

While testing on this web shell, I realized that;

 387420489L * 9 => Syntax error (at index 9). => does not accept type suffix
 (long)387420489 * 9 => -808182895 => overflow
 387420489 * (long)9 => 3486784401 => OK

 2147483647 + 1 => -2147483648 => int.MaxValue + 1 = int.MinValue (overflow)
 2147483648 + 1 => 2147483649 => When does not fit into Int32, interpreted as long

While most of these can be regarded as by design (considering how Dynamic Expresso evaluates the statement), there can still be further improvement.

Think of Javascript for example.

387420489*9 => 3486784401

The question is, is what we need

  1. to execute the given arithmetic expression correctly, as we and the end-user expects,
  2. to execute the given arithmetic expression the C# way?

The former, I think.

Oguz Ozgul
  • 6,809
  • 1
  • 14
  • 26