简体   繁体   English

为什么我收到System.OverflowException?

[英]Why am I getting the System.OverflowException?

This program is coded in c#. 该程序用c#编码。 It is supposed to display a 3d Graph of the function Log(x,y) . 应该显示一个function Log(x,y)的3d图形。 I don't know why but everytime I run it I get the System.OverflowException when it begins to draw the graph and the program stops. 我不知道为什么,但是每当我运行它时,当它开始绘制图形并且程序停止时,都会收到System.OverflowException

How can I prevent it from happening and why does it happen? 我如何防止它发生以及为什么会发生?

  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);
            }
    }

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

Quote: "Hi, the minimum value allowed for screen coordinate is x = -1073741376 and y = -1073740288, this is the boundary where the desktop surface lies, if you go further, you enter the void, therfore cause an overflow..." Quote:“嗨,屏幕坐标允许的最小值是x = -1073741376和y = -1073740288,这是桌面表面所在的边界,如果走得更远,您将输入空隙,从而导致溢出... “

I ran your calculation and besides the fact that it produces NaN and -Infinity values, it also produced very small values like -2147483503. 我进行了计算,除了生成NaN和-Infinity值外,还生成了非常小的值,如-2147483503。 This is the reason why you get the overflow exception. 这就是您收到溢出异常的原因。

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

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