Questions tagged [divide-by-zero]

Division by zero is the division by any value that is exactly equal to 0 -- a computation with an undefined result.

In computing, a program error may result from an attempt to divide by zero. Depending on the programming environment and the type of number (e.g. floating point, integer) being divided by zero, it may generate positive or negative infinity by the IEEE 754 floating point standard, generate an exception, generate an error message, cause the program to terminate, or result in a special NaN (not a number) value.

See also:

540 questions
237
votes
4 answers

How do I catch a numpy warning like it's an exception (not just for testing)?

I have to make a Lagrange polynomial in Python for a project I'm doing. I'm doing a barycentric style one to avoid using an explicit for-loop as opposed to a Newton's divided difference style one. The problem I have is that I need to catch a…
John K.
  • 2,473
  • 2
  • 13
  • 6
167
votes
9 answers

How to return 0 with divide by zero

I'm trying to perform an element wise divide in python, but if a zero is encountered, I need the quotient to just be zero. For example: array1 = np.array([0, 1, 2]) array2 = np.array([0, 1, 1]) array1 / array2 # should be np.array([0, 1, 2]) I…
hlin117
  • 20,764
  • 31
  • 72
  • 93
139
votes
6 answers

Avoid division by zero in PostgreSQL

I'd like to perform division in a SELECT clause. When I join some tables and use aggregate function I often have either null or zero values as the dividers. As for now I only come up with this method of avoiding the division by zero and null values.…
William Wino
  • 3,599
  • 7
  • 38
  • 61
110
votes
4 answers

What is the fastest integer division supporting division by zero no matter what the result is?

Summary: I'm looking for the fastest way to calculate (int) x / (int) y without getting an exception for y==0. Instead I just want an arbitrary result. Background: When coding image processing algorithms I often need to divide by an (accumulated)…
philipp
  • 1,745
  • 1
  • 14
  • 25
107
votes
5 answers

How do I avoid the "#DIV/0!" error in Google docs spreadsheet?

I have a column with average(K23:M23) that starts out with #DIV/0! when the K23 through M23 cells are empty. Preferably I'd like to only do the average of cells that contain non-zero, non-blank values. I think it's possible using the query…
Aaron
  • 2,154
  • 5
  • 29
  • 42
100
votes
7 answers

C++ warning: division of double by zero

Case 1: #include int main() { double d = 15.50; std::cout<<(d/0.0)< int…
Jayesh
  • 4,755
  • 9
  • 32
  • 62
84
votes
5 answers

Why doesn't 'd /= d' throw a division by zero exception when d == 0?

I don't quite understand why I don't get a division by zero exception: int d = 0; d /= d; I expected to get a division by zero exception but instead d == 1. Why doesn't d /= d throw a division by zero exception when d == 0?
80
votes
2 answers

Ignore divide by 0 warning in NumPy

I have a function for statistic issues: import numpy as np from scipy.special import gamma as Gamma def Foo(xdata): ... return x1 * ( ( #R is a numpy vector ( ((R - x2)/beta) ** (x3 -1) ) * …
overcomer
  • 2,244
  • 3
  • 26
  • 39
69
votes
5 answers

Why doesn't Java throw an Exception when dividing by 0.0?

I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when…
froadie
  • 79,995
  • 75
  • 166
  • 235
67
votes
4 answers

Why does C# allow dividing a non-zero number by zero in floating-point type?

Why C# allows: 1.0 / 0 // Infinity And doesn't allow: 1 / 0 // Division by constant zero [Compile time error] Mathematically, is there any differences between integral and floating-point numbers in dividing by zero?
Homam
  • 23,263
  • 32
  • 111
  • 187
66
votes
9 answers

Can't Mod Zero?

Why is X % 0 an invalid expression? I always thought X % 0 should equal X. Since you can't divide by zero, shouldn't the answer naturally be the remainder, X (everything left over)?
Dasaru
  • 2,828
  • 5
  • 25
  • 25
66
votes
5 answers

Can a near-zero floating value cause a divide-by-zero error?

Everybody knows you're not supposed to compare floats directly, but rather using a tolerance: float a,b; float epsilon = 1e-6f; bool equal = (fabs(a-b) < epsilon); I was wondering if the same applies to comparing a value to zero before using it in…
neuviemeporte
  • 6,310
  • 10
  • 49
  • 78
59
votes
7 answers

The behaviour of floating point division by zero

Consider #include int main() { double a = 1.0 / 0; double b = -1.0 / 0; double c = 0.0 / 0; std::cout << a << b << c; // to stop compilers from optimising out the code. } I have always thought that a will be +Inf, b…
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
54
votes
2 answers

Divide by zero and no error?

Just threw together a simple test, not for any particular reason other than I like to try to have tests for all my methods even though this one is quite straightforward, or so I thought. [TestMethod] public void Test_GetToolRating() { …
dinotom
  • 4,990
  • 16
  • 71
  • 139
47
votes
5 answers

Inconsistency in divide-by-zero behavior between different value types

Please consider the following code and comments: Console.WriteLine(1 / 0); // will not compile, error: Division by constant zero int i = 0; Console.WriteLine(1 / i); // compiles, runs, throws: DivideByZeroException double d =…
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
1
2 3
35 36