非法除以零
@xyVal = (4,4,6,6,10,12,18,22,24,28,30);
@yVal = (176,178,180,184,192,202,210,218,224,232,238);
@xxVal = (9,9,9,9,9 ,11,13,15,17,19,19);
@xVal = (168,166,164,162,158,150,142,134,122,116,110);
for ($i = 0; $i < scalar(@xVal); $i++){
for ($i = 0; @xyVal[$i] < @xxVal[$i]; $i++){
@yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);
}
}
print @yNewVal;
我明白为什么它给我关于第 9 行的错误非法除以零
(@yNewVal = ...),
如果零之间存在除法,我希望数组中有 0 。我做错了什么?那么,如何避免应用程序在被零除时崩溃呢?
@xyVal = (4,4,6,6,10,12,18,22,24,28,30);
@yVal = (176,178,180,184,192,202,210,218,224,232,238);
@xxVal = (9,9,9,9,9 ,11,13,15,17,19,19);
@xVal = (168,166,164,162,158,150,142,134,122,116,110);
for ($i = 0; $i < scalar(@xVal); $i++){
for ($i = 0; @xyVal[$i] < @xxVal[$i]; $i++){
@yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);
}
}
print @yNewVal;
I understand why its giving me the error Illegal division by zero
about line 9 (the @yNewVal = ...)
I want the array to have 0 in it if there is a division between zeros. What am I doing wrong? So, how can I avoid that my application crashes when there is a division by zero?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该行的除数为
@xyVal[$i] - @xyVal[$i-1]
,因此在任何情况下,@xyVAl
中有两个相同的相邻值(例如4,4
)将导致0,从而产生被零除错误。Your divisor on that line is
@xyVal[$i] - @xyVal[$i-1]
, so any case where you have two identical adjacent values in@xyVAl
(e.g.4,4
) will result in a 0, and thus a divide-by-zero error.你可以说:
You could say:
好吧,如果我理解正确的话:
well if i understand you correctly:
您可以使用
eval
和条件运算符执行 try/catch。不过,您的短语返回一个标量值并将其放入数组变量中。所以你可能想要重新考虑这一点。
You can perform a try/catch using
eval
and conditional operators.Though, your phrase is returning a scalar value and putting it into an array variable. So you may want to re-factor that.