Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define division by zero as infinity

Tags:

c

I want to define the result of division by zero as the double INF.

There are some discussions about the default behavior in C/C++ for division by zero. No question (that I read) asks explicitly how to define the division by zero to become infinity in C. Whether this makes sense or not I would rather not discuss. I just want to define it that way for one file with several C functions in it and need the syntax for it.

like image 206
hcl734 Avatar asked Jun 18 '19 08:06

hcl734


People also ask

Why is division by zero infinity?

The reason that the result of a division by zero is undefined is the fact that any attempt at a definition leads to a contradiction. a=r*b. r*0=a. (1) But r*0=0 for all numbers r, and so unless a=0 there is no solution of equation (1).

What is the answer of infinity divided by 0?

Thus infinity/0 is a problem both because infinity is not a number and because division by zero is not allowed.

How is the division by zero defined?

Because what happens is that if we can say that zero, 5, or basically any number, then that means that that "c" is not unique. So, in this scenario the first part doesn't work. So, that means that this is going to be undefined. So zero divided by zero is undefined.

Is division by Infinity defined?

In mathematics, division by infinity is division where the divisor (denominator) is infinity.


2 Answers

If you require this behaviour, use floating point numbers, which can represent infinity, and provide the desired behaviour. Note that technically this is undefined behaviour but in practice most compilers (all mainstream compilers for standard architectures) implement IEEE 754 semantics, e.g. GCC.

int main() {
    float f = 42;
    float g = f / 0.0f;
    printf("%f\n", g);
}

Output:

inf

This is behaviour that can be relied on since it’s clearly documented by the compilers. However, when writing portable code make sure that you test these assumptions inside your code (e.g. by testing whether the preprocessor macro __STDC_IEC_559__, as well as compiler-specific macros are defined).

If, for some reason, you need this behaviour for integer values, the only recourse is to make your own type. Something like this:

typedef struct {
    int value;
    bool is_inf;
    bool is_nan;
} ext_int;

ext_int make_ext_int(int i) {
    return (ext_int) {i, false, false};
}

ext_int make_nan() {
    return (ext_int) {0, false, true};
}

ext_int make_inf(int sign) {
    return (ext_int) {(sign > 0) - (sign < 0), true, false};
}

ext_int ext_div(ext_int a, ext_int b) {
    if (a.is_nan || b.is_nan) {
        return  make_nan();
    }
    if (b.value == 0) {
        return make_inf(a.value);
    }
    // TODO: insert other cases.
    return (ext_int) {a.value / b.value, false, false};
}

… in a real implementation you’d pack the different flags rather than having a separate bool for each, of course.

like image 120
Konrad Rudolph Avatar answered Sep 17 '22 19:09

Konrad Rudolph


Floating point division by zero is undefined by the C standard.

(IEEE754 - common but by no means ubiquitous - defines a / 0.0 to be +INF if a is positive, -INF if a is negative and NaN if a is also zero).

Your best bet is to define a function that models the division operator, and implement your behaviour there.

like image 43
Bathsheba Avatar answered Sep 20 '22 19:09

Bathsheba