question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specify zero division behavior

See original GitHub issue

numpy and PyTorch diverge on the zero division behavior of integral dtype’s:

>>> a1 = np.array(1, dtype=np.int)
>>> a2 = np.array(0, dtype=np.int)
>>> a1 // a2
0
>>> a1 % a2
0
>>> t1 = torch.tensor(1, dtype=torch.int)
>>> t2 = torch.tensor(0, dtype=torch.int)
>>> t1 // t2
RuntimeError: ZeroDivisionError
>>> t1 % t2
RuntimeError: ZeroDivisionError

We should either specify the behavior or fix the test suite to exclude zero as second input when testing these functions. cc @asmeurer

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
asmeurercommented, Aug 31, 2021

Yes, just a note is what I’m expecting. For the test suite I am completely skipping testing zero inputs for x2 for floor_divide and remainder(). The spec says “Rounds the result of dividing each element x1_i of the input array x1 by the respective element x2_i of the input array x2 …” which is meaningless if x2_i is 0 and x2 has an integer dtype.

1reaction
rgommerscommented, Jun 12, 2021

The numpy code does give:

>>> a1 // a2
<ipython-input-42-136abf062072>:1: RuntimeWarning: divide by zero encountered in floor_divide
  a1 // a2
0
>>> a1 % a2
<ipython-input-43-bdabd79cdfbc>:1: RuntimeWarning: divide by zero encountered in remainder
  a1 % a2
0

There’s an issue about it here: https://github.com/numpy/numpy/issues/5150

I don’t think this should be specified in the array API spec, it falls under the warnings/exceptions behavior which is library-specific.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Division by zero
In mathematics, division by zero is division where the divisor (denominator) is zero. ... In these cases, if some special behavior is desired...
Read more >
Division by zero | Apple Developer Documentation
Division by zero has undefined behavior, and can result in crashes or incorrect program output. ... Detects when you incorrectly assign null to...
Read more >
Will a computer attempt to divide by zero?
division by zero invokes undefined behaviour. So according to the language definition, anything can happen. Especially things that you don't want to happen....
Read more >
Division by Zero in Java: Exception, Infinity, or Not a Number
When dividing by zero, integer division always throws an Exception. This is not the case with floating-point numbers, however.
Read more >
Detect division by zero
Detect division by zero ... Write a function to detect a divide by zero error without checking if the denominator is zero. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found