4

While trying to find a simple C# math calculator, I have found one below. C# Math calculator

It is ideal for me since it does not need any COM or other third-party reference. However, I find out a confusing thing:

new DataTable().Compute("2-3/4*12", null) // Works fine, answer is -7;
new DataTable().Compute("2 / 0", null)    // Works but not correct, answer is 8;
new DataTable().Compute("100 / 0", null)  // Works but not correct, answer is 8;
new DataTable().Compute("2.0 / 0", null)  // Throw System.DivideByZeroException as estimated.

So I would like to ask, what's wrong with the second and third lines? Is the compute method treat the "/" is another way?

I have read this: https://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx, but nothing helpful.

  • See [here](http://stackoverflow.com/questions/4609698/inconsistency-in-divide-by-zero-behavior-between-different-value-types). It has something to do with the datatypes that compute uses in background. If you would try `var divByZero = 2 / 0;` you would get compile error – Nitro.de Mar 29 '16 at 06:11
  • @Nitro.de I think my problem is different from the one you referred to. Since I does not declare any double or int, I just use writeline to see the result. –  Mar 29 '16 at 06:50
  • Right you dont, but do you know what compute does in background? – Nitro.de Mar 29 '16 at 06:54
  • @Nitro.de So it should be converted to the corresponding type? By the way, I have found out a workaround. For example, if the code is:Expression = "2 / 0"; new DataTable(Expression).Compute(Expression, null), I can make it changed to: new DataTable().Compute("1.0 * " + Expression, null) and then it can work well. –  Mar 29 '16 at 16:05

2 Answers2

1

I Tried your code in LinqPad with the following results:

new DataTable().Compute("2 / 0", null); // Gives infinity (object{double})
new DataTable().Compute("100 / 0", null); // Gives infinity (object{double})
new DataTable().Compute("100 / 0", null); // Gives divideByZeroException

From my testcode:

        var result = (double) new DataTable().Compute("20 / 0", null);

        if(double.IsInfinity(result))
        {
            Console.WriteLine("Oh noes!"); // Debugger lands here
        }
        Console.WriteLine(result);
    }

The concept of infinity exists in double. I'm not sure why you get the exception on the last one though. Hopefully someone can comment on that.

Pedro G. Dias
  • 3,162
  • 1
  • 18
  • 30
  • Hi @pedro-g-dias, did you mean to have "100.0 / 0" on your third expression here? For me, it seems there's a difference between the integer and float calculations. Using the float, the exception is thrown. – CJBrew Dec 15 '16 at 10:14
0

Part of this question has already been answered: Why is infinity printed as "8" in the Windows 10 console?

We have an application that on Windows 8.1 will print "Infinity" for an expression such as "1 / 0". On Windows 10, it returns "8".

It's not a numeric value of 8, but an incorrectly displayed Unicode character (which should be a dead-8).

Community
  • 1
  • 1
CJBrew
  • 2,720
  • 1
  • 20
  • 27