简体   繁体   English

关于 C# DataTable.Compute() 的语法

[英]About the syntax of C# DataTable.Compute()

While trying to find a simple C# math calculator, I have found one below.在试图找到一个简单的 C# 数学计算器时,我在下面找到了一个。 C# Math calculator C# 数学计算器

It is ideal for me since it does not need any COM or other third-party reference.它对我来说非常理想,因为它不需要任何 COM 或其他第三方参考。 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.我读过这个: https://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx ,但没有任何帮助。

I Tried your code in LinqPad with the following results:我在 LinqPad 中尝试了您的代码,结果如下:

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.希望有人可以对此发表评论。

Part of this question has already been answered: Why is infinity printed as "8" in the Windows 10 console?这个问题的一部分已经得到回答: 为什么在 Windows 10 控制台中无穷大打印为“8”?

We have an application that on Windows 8.1 will print "Infinity" for an expression such as "1 / 0".我们有一个应用程序,它在 Windows 8.1 上会为诸如“1 / 0”之类的表达式打印“Infinity”。 On Windows 10, it returns "8".在 Windows 10 上,它返回“8”。

It's not a numeric value of 8, but an incorrectly displayed Unicode character (which should be a dead-8).它不是数字值 8,而是一个错误显示的 Unicode 字符(应该是一个死 8)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM