为什么我收到System.OverflowException?

时间:2018-11-23 13:30:45

标签: c# integer-overflow

该程序用c#编码。应该显示System.OverflowException的3d图形。我不知道为什么,但是每次运行它时, private void Draw_Function_Click(object sender, EventArgs e) { int size = 100; double accuracy = 0.09; int zoom = 1; ver = new double[size, size]; xtag = new double[size, size]; ytag = new double[size, size]; function = Insert_Function.Text; for (int i = 0; i < 100; i++) { for (int p = 0; p < 100; p++) { ver[i, p] = Math.Log(i,p); xtag[i, p] = p * accuracy - i * accuracy * Math.Cos(Math.PI / 5); ytag[i, p] = ver[i, p] - i * accuracy * Math.Sin(Math.PI / 5); } } Graphics g = panel1.CreateGraphics(); for (int i = 0; i < ver.GetLength(0) - 1; i++) for (int p = 1; p < ver.GetLength(1) - 1; p++) { int y0 = (panel1.Height / 2) - (int)(ytag[i, p] * zoom); int x0 = (int)(zoom * xtag[i, p]) + panel1.Width / 2; int y1 = (panel1.Height / 2) - (int)(ytag[i + 1, p + 1] * zoom); int x1 = (int)(zoom * xtag[i + 1, p + 1]) + panel1.Width / 2; g.DrawLine(Pens.Black, (float)x0, (float)y0, (float)x1, (float)y1); } } 就会开始绘制图形,并且程序停止。

如何防止它发生以及为什么发生?

rcm_data

1 个答案:

答案 0 :(得分:0)

我找到了以下答案:https://social.msdn.microsoft.com/Forums/windows/en-US/9f2b5bba-f725-45c1-9ada-383151267c13/overflow-exception-on-drawline-method-?forum=winforms

  

引用:“嗨,屏幕坐标允许的最小值是x = -1073741376和y = -1073740288,这是桌面表面所在的边界,如果走得更远,则输入空白,因此会导致溢出...”

我进行了计算,除了产生NaN和-Infinity值外,还产生了非常小的值,如-2147483503。这就是您收到溢出异常的原因。