如何检查c中是否有被零除的情况
#include<stdio.h>
void function(int);
int main()
{
int x;
printf("Enter x:");
scanf("%d", &x);
function(x);
return 0;
}
void function(int x)
{
float fx;
fx=10/x;
if(10 is divided by zero)// I dont know what to put here please help
printf("division by zero is not allowed");
else
printf("f(x) is: %.5f",fx);
}
#include<stdio.h>
void function(int);
int main()
{
int x;
printf("Enter x:");
scanf("%d", &x);
function(x);
return 0;
}
void function(int x)
{
float fx;
fx=10/x;
if(10 is divided by zero)// I dont know what to put here please help
printf("division by zero is not allowed");
else
printf("f(x) is: %.5f",fx);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这应该可以做到。在执行除法之前,您需要检查是否被零除。
This should do it. You need to check for division by zero before performing the division.
默认情况下,在 UNIX 中,浮点除以零不会因异常而停止程序。相反,它会产生
无穷大
或NaN
的结果。您可以使用isfinite
检查这些是否都发生。或者,您可以检查除数是否不为零:
By default in UNIX, floating-point division by zero does not stop the program with an exception. Instead, it produces a result which is
infinity
orNaN
. You can check that neither of these happened usingisfinite
.Alternately, you can check that the divisor isn't zero:
使用 C99,您可以使用
fetestexcept(2)
等。With C99 you can use
fetestexcept(2)
et alia.