# No more ZeroDivisionError

**URL:** <https://discuss.python.org/t/no-more-zerodivisionerror/69328>\
**Category:** Ideas\
**Created:** [October 27, 2024, 5:46am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328 "2024-10-27T05:46:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:46am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/1 "2024-10-27T05:46:02Z")

</div>

Let Python just not reveal that useless error. In real life X/0 will always give 0.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 27, 2024, 5:48am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/2 "2024-10-27T05:48:39Z")

</div>

> [@boludoz](#):
>
> In real life X/0 will always give 0.

That’s so completely untrue that it’s hard to even begin to answer. No, dividing by zero does not give zero.

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:51am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/3 "2024-10-27T05:51:09Z")

</div>

I don’t really see much point in this, maybe we could send a warning?

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:52am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/4 "2024-10-27T05:52:54Z")

</div>

It is conventional that 0/0 is always 0. Imagine a child is learning to divide with Python and this error appears.

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:55am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/5 "2024-10-27T05:55:14Z")

</div>

Anyway X / 0 will always give an error in Python, I think your point makes no sense.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 27, 2024, 5:56am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/6 "2024-10-27T05:56:19Z")

</div>

> [@boludoz](#):
>
> It is conventional that 0/0 is always 0.

I’m not going to cast aspersions on the quality of your education, but it does seem to disagree with the rest of the world:

> **[Division by Zero -- from Wolfram MathWorld](https://mathworld.wolfram.com/DivisionbyZero.html)**
>
> Division by zero is the operation of taking the quotient of any number x and 0, i.e., x/0. The uniqueness of division breaks down when dividing by zero, since the product 0·y=0 is the same for any y, so y cannot be recovered by inverting the process...

> **[Division by zero](https://en.wikipedia.org/wiki/Division_by_zero)**
>
> In mathematics, division by zero, division where the divisor (denominator) is zero, is a unique and problematic special case. Using fraction notation, the general example can be written as 
>   
>     
>       
>         
>           
>             
> a
> 0
>             
>           
>         
>       
>     
> {\\displaystyle {\\tfrac {a}{0}}}
>   
> , where 
>   
>     
>       
> a
>       
>     
> {\\displaystyle a}
>   
> is the dividend (numerator).
> The usual definition of the quotient in ele...

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:56am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/7 "2024-10-27T05:56:44Z")

</div>

Anyway X / 0 will always raise an error in Python, I think your point is nonsense. My brain doesn’t raise an error when I’m dividing a variable by zero.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 27, 2024, 5:58am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/8 "2024-10-27T05:58:09Z")

</div>

> [@boludoz](#):
>
> My brain doesn’t raise an error when I’m dividing a variable by zero.

Then you’re welcome to write MyBrainPython that behaves the way your brain does. I’ll keep using the Python that follows the straight-forward rule that division by zero can’t be done.

There are ways to give a meaningful value to division by zero (such as limits), but the result is seldom zero.

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 5:59am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/9 "2024-10-27T05:59:48Z")

</div>

Not even C++ throws an error when I’m dividing by 0, it doesn’t seem correct to me.

---

<div class="post-metadata">

**Author:** ![boludoz](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/boludoz/32/15870_2.png) [@boludoz](https://discuss.python.org/u/boludoz)\
**Post date:** [October 27, 2024, 6:05am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/10 "2024-10-27T06:05:09Z")

</div>

My mother is a math teacher, I have been programming since I was 6 years old and I have never seen such a silly mistake: 1 / 0 is always 0 as 0 x 1 is always 0, you can’t divide nothing by nothing.

Infinity is like God, it’s there but I’ve never seen it.

Maybe it would be nice to return a variable representing ‘nan’ that has the value 0. Like some kind of abstraction.

---

<div class="post-metadata">

**Author:** ![Nineteendo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nineteendo/32/19122_2.png) [@Nineteendo](https://discuss.python.org/u/Nineteendo)\
**Post date:** [October 27, 2024, 7:15am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/11 "2024-10-27T07:15:55Z")

</div>

Python does simply what most languages do and none of the languages I tested return 0:

R, JavaScript: `-1/0 = -Infinity`, `0/0 = NaN`, `1/0 = Infinity`  
Python, C, C++, C#, Java, Go, PHP, Swift, Rust: `-1/0, 0/0, 1/0 -> error`

Suggesting to return `-inf`, `nan` or `inf` would have a bigger chance at success, but probably still won’t happen.

---

<div class="post-metadata">

**Author:** ![da-woods](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/da-woods/32/7507_2.png) [@da-woods](https://discuss.python.org/u/da-woods)\
**Post date:** [October 27, 2024, 7:38am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/12 "2024-10-27T07:38:33Z")

</div>

Given that Python now does give float results for integer division it’d make sure sense to extend that to returning NaN or infinity. But I’d be surprised if it happened

---

<div class="post-metadata">

**Author:** ![ayhanfuat](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ayhanfuat/32/21125_2.png) [@ayhanfuat](https://discuss.python.org/u/ayhanfuat)\
**Post date:** [October 27, 2024, 8:01am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/13 "2024-10-27T08:01:42Z")

</div>

> [@boludoz](#):
>
> I have been programming since I was 6 years old and I have never seen such a silly mistake: 1 / 0 is always 0

Which programming language is that? The one where 1/0 = 0?

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [October 27, 2024, 9:13am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/14 "2024-10-27T09:13:20Z")

</div>

> [@boludoz](#):
>
> Not even C++ throws an error when I’m dividing by 0

That is because the programmer is expected to check for zero before attempting the divide.  
This is an optimisation according to the designers of C++.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 27, 2024, 9:15am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/15 "2024-10-27T09:15:52Z")

</div>

> [@barry-scott](#):
>
> That is because the programmer is expected to check for zero before attempting the divide.

I’m curious what your C++ compiler does. Mine (GCC on Linux with default settings) terminates with SIGFPE, which seems pretty appropriate.

---

<div class="post-metadata">

**Author:** ![ajoino](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ajoino/32/6907_2.png) [@ajoino](https://discuss.python.org/u/ajoino)\
**Post date:** [October 27, 2024, 10:10am UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/16 "2024-10-27T10:10:48Z")

</div>

Seems to be undefined behaviour except if the numbers are IEEE floats:

> 1. The result of built-in division is lhs divided by rhs. If rhs is zero, the behavior is undefined.
> 
> If both operands have an integral type, the result is the algebraic quotient (performs integer division): the quotient is truncated towards zero (fractional part is discarded).
> 
> If both operands have a floating-point type, and the type supports IEEE floating-point arithmetic (see [std::numeric\_limits::is\_iec559](https://en.cppreference.com/w/cpp/types/numeric_limits/is_iec559)):
> 
> - If one operand is NaN, the result is NaN.
> - Dividing a non-zero number by ±0.0 gives the correctly-signed infinity and [FE\_DIVBYZERO](https://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions) is raised.
> - Dividing 0.0 by 0.0 gives NaN and [FE\_INVALID](https://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions) is raised.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [October 27, 2024, 1:14pm UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/17 "2024-10-27T13:14:23Z")

</div>

> [@Rosuav](#):
>
> I’m curious what your C++ compiler does. Mine (GCC on Linux with default settings) terminates with SIGFPE, which seems pretty appropriate.

I web searched to see what C++ defined and I hit SO topics on how to raise exceptions. That explained that by default there are no exceptions from the C++ language definition.  
SIGFPE is a CPU exception coming from the kernel into user space.

For this code:

```python
#include <cstdio>

int main() {
    float a = 19.3;
    float b = 0.0;

    printf("%f\n", a/b);
    return 0;
}

```

It prints “inf” for llvm on macOS, g++ on Fedora and also for Microsoft Visual C++.

For this int version:

```python
#include <cstdio>

int div(int a, int b) {
    printf("result: %d\n", a/b);
    return a/b;
}

int main() {
    int a = 19;
    int b = 0;

    div(a, b);
    return 0;
}

```

Microsoft Visual C++ exits, not output, not error message.  
g++ report “floating point expection”  
macOS llvm prints “0”.

That’s a surprising, to me, set of results.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [October 27, 2024, 1:21pm UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/18 "2024-10-27T13:21:55Z")

</div>

> [@boludoz](#):
>
> In real life X/0 will always give 0.

In what real life situations do you divide by 0?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 27, 2024, 1:33pm UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/19 "2024-10-27T13:33:53Z")

</div>

Small tip: If I want to ensure the compiler won’t optimize things away, I make use of argc in the expression.

```python
#include <iostream>
int main(int argc, char **argv) {
    std::cout << 24 / (argc - 1) << std::endl;
}

```

This also gave me a floating point exception in G++, but I don’t have MSVC to test on, so I don’t know if your “no output, no error” was the result of the compiler optimizing away the actual calculation.

---

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [October 27, 2024, 1:55pm UTC](https://discuss.python.org/t/no-more-zerodivisionerror/69328/20 "2024-10-27T13:55:10Z")

</div>

First, it is a [logical error](https://en.wikipedia.org/wiki/Logic_error). If a programmer mistakenly writes code that divides by zero, it suggests they haven’t properly accounted for cases where the divisor could be zero.

Second, division by zero is undefined in mathematics, and depending on the field or context, various interpretations or conventions are applied to handle it. It could be 0, ∞, -∞, or still undefined.

So, being both a logical error and undefined, it must be handled according to the field or context. To avoid hard-to-debug bugs, the best approach is to raise a `ZeroDivisionError` exception.

[Next page](https://discuss.python.org/t/no-more-zerodivisionerror/69328.md?page=2)
