【发布时间】:2018-11-23 13:30:44
【问题描述】:
这个程序是用 c# 编码的。它应该显示function Log(x,y) 的 3d 图形。我不知道为什么,但每次我运行它时,当它开始绘制图形并且程序停止时,我都会得到System.OverflowException。
我怎样才能防止它发生?为什么会发生?
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);
}
}
【问题讨论】:
-
请将您的代码的文本版本粘贴到问题的正文中。
-
查看引发异常的行,您的算术运算或强制转换产生的结果对于目标类型来说太大了。
-
@AlexK。我应该怎么做才能使其值适合目标类型?
-
我首先要看的是对数;未定义零日志(编辑:至少在数学上)...并且以零为底的日志...这是什么意思?即
ver[i, p] = Math.Log(i,p);当i和p都是0? -
@MarcGravell 零日志定义为
NegativeInfinity:docs.microsoft.com/en-us/dotnet/api/…
标签: c# integer-overflow