简体   繁体   English

(int)Math.Pow(10,0)返回零而不是一

[英](int) Math.Pow(10, 0) returning zero rather than one

I'm attempting to reverse an integer and for some reason I receive a RunTime error saying "System.DivideByZeroException: Attempted to divide by zero." 我试图反转整数,由于某种原因,我收到一个运行时错误消息,提示“ System.DivideByZeroException:试图除以零。” Having figured out that Math.Pow returns a double , why doesn't the conversion to an int maintain the result of "1". 弄清楚Math.Pow返回double ,为什么不将转换为int的结果保持为“ 1”。 10 to the 0 is 1, and 1 converting to 1 should still be 1 even if you include any rounding. 从10到0为1,即使包含任何舍入,从1到1的转换仍应为1。 Anybody have a good explanation for this? 有人对此有很好的解释吗?

if (x > 0)
        {
            var i = 0;
            while (x > 0)
            {
                int endDig = x % (10 * ((int)Math.Pow(10, i)));
                myNum += endDig.ToString();
                i += 1;
            }
            int newNum = Int32.Parse(myNum);
            return newNum;
        }

The reason of divide by zero here is not really related to truncating result when casting Math.Pow(x, 0) to int. 此处被零除的原因与将Math.Pow(x, 0)强制Math.Pow(x, 0)为int时的截断结果并不真正相关。 Math.Pow(x, 0) will always return 1, and casting it to int will give you 1. Reason is integer overflow. Math.Pow(x, 0)将始终返回1,并将其强制转换为int将给您1。原因是整数溢出。 For example consider: 例如考虑:

var pow = (int)Math.Pow(10, 10);

10^10 does not fit into int size, so it will overflow (multiple times), and variable pow will have value -2147483648 . 10 ^ 10不适合int大小,因此它将溢出(多次),并且变量pow将具有值-2147483648 When you multiply that by 10, it again overflows and result is 0. At this point you get your divide by zero exception. 当您将其乘以10时,它再次溢出,结果为0。这时,您将得到除以零的异常。

To avoid such bizzare results you might want to do arithmetic operations that might unexpectedly result in overflow in checked context: 为了避免出现这种奇怪的结果,您可能需要进行算术运算,以免在检查的上下文中导致溢出:

checked {
    var pow = (int) Math.Pow(10,10); 
    // throws overflow exception so you can fix your buggy code early
}

Of course at that point you already realize that your algorithm to reverse number is not a good one, so refer to another answer for a better way. 当然,到那时,您已经意识到,将数字求反的算法不是一个好方法,因此请参考另一种答案以寻求更好的方法。

You are encountering limitations of computer-based arithmetic. 您遇到了基于计算机的算术限制。 (Another answer shows the details which I were incorrect in previous versions of my answer.) To avoid these details entirely, you can divide by 10 at the end of each iteration of the loop: (另一个答案显示了我在以前版本的答案中不正确的细节。)要完全避免这些细节,可以在循环的每次迭代结束时将其除以10:

while (x > 0)
{
    int endDig = x % 10;
    //...
    x /= 10;
}

Now you no longer need to worry the limits of int because your number will never be larger than the original input. 现在您不再需要担心int的限制,因为您的数字永远不会大于原始输入。

Math.Pow(10, 0) returns 1 . Math.Pow(10,0)返回1。

在此处输入图片说明

I dont think that it returns 0.9999 and by casting it to int its getting truncated. 我不认为它会返回0.9999并通过将其强制转换为整数而被截断。

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

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