非法除以零

发布于 2024-12-11 10:14:15 字数 574 浏览 0 评论 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;

我明白为什么它给我关于第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恰似旧人归 2024-12-18 10:14:15

该行的除数为 @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.

勿忘初心 2024-12-18 10:14:15

你可以说:

@yNewVal = ($_ = @xyVal[$i] - @xyVal[$i-1]) == 0 ? 0 : @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/$_;

You could say:

@yNewVal = ($_ = @xyVal[$i] - @xyVal[$i-1]) == 0 ? 0 : @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/$_;
伪装你 2024-12-18 10:14:15

好吧,如果我理解正确的话:

if (@xyVal[$i] == @xyVal[$i-1]) 
@yNewVal = 0; 
else 
@yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);

well if i understand you correctly:

if (@xyVal[$i] == @xyVal[$i-1]) 
@yNewVal = 0; 
else 
@yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);
半步萧音过轻尘 2024-12-18 10:14:15

您可以使用 eval 和条件运算符执行 try/catch。

eval {
    @yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);
    1;
} or do {
    @yNewVal = (0);
}; 
print @yNewVal;

不过,您的短语返回一个标量值并将其放入数组变量中。所以你可能想要重新考虑这一点。

You can perform a try/catch using eval and conditional operators.

eval {
    @yNewVal = @yVal[$i-1] + (@yVal[$i] - @yVal[$i-1])*(@xxVal[$i] - @xyVal[$i-1])/(@xyVal[$i] - @xyVal[$i-1]);
    1;
} or do {
    @yNewVal = (0);
}; 
print @yNewVal;

Though, your phrase is returning a scalar value and putting it into an array variable. So you may want to re-factor that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文