如何检查c中是否有被零除的情况

发布于 2024-08-26 03:45:48 字数 398 浏览 3 评论 0原文

#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 技术交流群。

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

发布评论

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

评论(4

往日情怀 2024-09-02 03:45:48
#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;

    if(x==0) // Simple!
        printf("division by zero is not allowed");
    else
        fx=10/x;            
        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;

    if(x==0) // Simple!
        printf("division by zero is not allowed");
    else
        fx=10/x;            
        printf("f(x) is: %.5f",fx);

}
断爱 2024-09-02 03:45:48

这应该可以做到。在执行除法之前,您需要检查是否被零除。

void function(int x)
{
    float fx;

    if(x == 0) {
        printf("division by zero is not allowed");
    } else {
        fx = 10/x;
        printf("f(x) is: %.5f",fx);
    }
}

This should do it. You need to check for division by zero before performing the division.

void function(int x)
{
    float fx;

    if(x == 0) {
        printf("division by zero is not allowed");
    } else {
        fx = 10/x;
        printf("f(x) is: %.5f",fx);
    }
}
骷髅 2024-09-02 03:45:48

默认情况下,在 UNIX 中,浮点除以零不会因异常而停止程序。相反,它会产生无穷大NaN的结果。您可以使用 isfinite 检查这些是否都发生。

x = y / z; // assuming y or z is floating-point
if ( ! isfinite( x ) ) cerr << "invalid result from division" << endl;

或者,您可以检查除数是否不为零:

if ( z == 0 || ! isfinite( z ) ) cerr << "invalid divisor to division" << endl;
x = y / z;

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 or NaN. You can check that neither of these happened using isfinite.

x = y / z; // assuming y or z is floating-point
if ( ! isfinite( x ) ) cerr << "invalid result from division" << endl;

Alternately, you can check that the divisor isn't zero:

if ( z == 0 || ! isfinite( z ) ) cerr << "invalid divisor to division" << endl;
x = y / z;
夏雨凉 2024-09-02 03:45:48

使用 C99,您可以使用 fetestexcept(2) 等。

With C99 you can use fetestexcept(2) et alia.

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