Specify zero division behavior
See original GitHub issuenumpy 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:
- Created 2 years ago
- Comments:15 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 arrayx1
by the respective elementx2_i
of the input arrayx2
…” which is meaningless if x2_i is 0 and x2 has an integer dtype.The numpy code does give:
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.