Skip to content

Make ZeroDivisionError message more precise when floor-dividing by zero #119057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
E8zEbo8Luna opened this issue May 14, 2024 · 7 comments
Closed
Labels
type-feature A feature request or enhancement

Comments

@E8zEbo8Luna
Copy link

E8zEbo8Luna commented May 14, 2024

Feature or enhancement

Proposal:

try:
  10 // 0
except ZeroDivisionError as e:
  print(e) #returns integer division or modulo by zero

try:
  10 / 0
except ZeroDivisionError as e:
  print(e) #returns integer division by zero

try:
  10 % 0
except ZeroDivisionError as e:
  print(e) #returns integer modulo by zero

When floor dividing by zero the error says "division or modulo" but while (non-floor) dividing by zero or modulo-ing (?) it is more specific saying "division" or "modulo". I would recommend changing this to "floor division" or "division" because it would help with knowing what exactly the error is talking about.

Thank You!

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@E8zEbo8Luna E8zEbo8Luna added the type-feature A feature request or enhancement label May 14, 2024
@AlexWaygood AlexWaygood changed the title Remove redundant ZeroDivisionError details Make error message more precise when floor-dividing by zero May 15, 2024
@AlexWaygood AlexWaygood changed the title Make error message more precise when floor-dividing by zero Make ZeroDivisionError message more precise when floor-dividing by zero May 15, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue May 15, 2024
sobolevn added a commit to sobolevn/cpython that referenced this issue May 15, 2024
@serhiy-storchaka
Copy link
Member

I was thinking about it yesterday. Perhaps a better solution would be dropping " or modulo" and leaving only "integer division by zero" for x / 0, x // 0 and divmod(x, 0). It does not make difference whether it is a true division, a floor division, or if the modulo is involved, -- it is all division by zero.

But if make the error message more specific (add "floor"), should not it also replace "division or modulo" with "division and modulo" for divmod(x, 0)?

Whatever be done for integers, should be done for other numerical types.

cc @mdickinson

@mdickinson
Copy link
Member

I see the value in harmonising the error messages, but I suspect we're all going to have different opinions on what the message should be. I'd personally drop the mention of the type, so that both 10 // 0 and 10.0 // 0 would give message "floor division by zero". (And as a bonus, fractions.Fraction(10) // 0 would also give message floor division by zero, simply by propagating the error message from the internal integer division.)

So if we're changing this, I'd go for:

  • 10 // 0, 10.0 // 0 - "floor division by zero"
  • 10 / 0, 10.0 // 0 - "division by zero"
  • 10 % 0, 10.0 % 0 is tricky: "modulo by zero" doesn't really work grammatically. But there's an implicit division, so "division by zero" doesn't seem wrong.

Would it be terrible to simply say "division by zero" for all these cases? No mention of type; no distinguishing between the /, //, % operators and divmod.

@mdickinson
Copy link
Member

mdickinson commented May 15, 2024

@serhiy-storchaka

Perhaps a better solution would be dropping " or modulo" and leaving only "integer division by zero" for x / 0, x // 0 and divmod(x, 0). It does not make difference whether it is a true division, a floor division, or if the modulo is involved, -- it is all division by zero.

+1

@serhiy-storchaka
Copy link
Member

If it helps to harmonize the error messages, I support dropping also the type. The only difference may be if mixed types are involved, and the non-zero divisor is converted with losses to the zero of other type. But AFAIK there are no such cases in the core and the stdlib.

In the end, all ZeroDivisionError exceptions may have the same error message.

@sobolevn
Copy link
Member

Ok, I agree. I will change my PR 👍

@sobolevn
Copy link
Member

I think that I found one case that should not be changed:

>>> 0 ** -1
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    0 ** -1
    ~~^^~~~
ZeroDivisionError: 0.0 cannot be raised to a negative power

I would not want to see "division by zero" here. In fact, it will be just 1 / 0, but still it won't be very clear.

I also found that 0.0 is used when 0 ** -1

@mdickinson
Copy link
Member

Agreed that the power case shouldn't be changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants