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
5
votes
1 answer

Divide by zero with loop optimization VC++ 2015

Prehistory: We have just switched our developing environment to VC++ 2015 from VC++ 2008. Right after that we have found the issue: program raised division by 0 despite testing divider in C++ code. Test code: #include int test(int n, int…
5
votes
1 answer

How to prevent division by zero or replace infinite values in Theano?

I'm using a cost function in Theano which involves a regularizer term that requires me to compute this term: T.sum(c / self.squared_euclidean_distances) as some values of self.squared_euclidean_distances might be zero this produces Nan values. How…
Ash
  • 3,428
  • 1
  • 34
  • 44
5
votes
4 answers

divide by zero error

here is the code (java): class prime { public static boolean prime (int a, int b) { if (a == 0) { return false; } else if ((a%(b-1) == 0) && (b>2)) { return false; …
David
  • 14,569
  • 34
  • 78
  • 107
5
votes
11 answers

How different programming languages handle division by 0?

Perhaps this is the wrong sort of question to ask here but I am curious. I know that many languages will simply explode and fail when asked to divide by 0, but are there any programming languages that can intelligently handle this impossible sum -…
Jack
  • 9,615
  • 18
  • 72
  • 112
5
votes
5 answers

Why is number divided by zero infinity in Java?

Why should the following code in Java System.out.println(new Integer(1)/ new Double(0)); print 'Infinity' and not undefined. Isn't that mathematically wrong?
The Governor
  • 1,152
  • 1
  • 12
  • 28
5
votes
3 answers

Preventing dividing by zero in list comprehensions

I have the following code: scores = [matrix[i][i] / sum(matrix[i]) for (i, scores) in enumerate(matrix)] My problem is that sum(matrix[i]) could be 0 in some cases, resulting in a ZeroDivisionError. But because matrix[i][i] is also 0 in that case,…
Lewistrick
  • 2,649
  • 6
  • 31
  • 42
5
votes
1 answer

Avoiding weird homography values when normalizing

People familiar with Homographies will know that you have to normalize it dividing by any of the matrix components in order to keep homogeneous coordinates. An homography is a 3x3 matrix and it is usually normalized dividing by the element at…
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
4
votes
2 answers

What is __alldiv?

I receive error reports with a Division by zero crash, and the crash occurs at a function called __alldiv. This function is not called anywhere in my code, I searched for it with Find in files.
sashoalm
  • 75,001
  • 122
  • 434
  • 781
4
votes
2 answers

Is x != 0.0 a safe check against DivideByZeroException?

In order to prevent DivideByZeroException in C#, people often write things like double f(double x) { if (x != 0.0) return 1000.0/x; else return 0.0; } Given the fact that floating-point arithmetic always has imprecisions, I wonder whether it is…
Kit Fisto
  • 4,385
  • 5
  • 26
  • 43
4
votes
1 answer

how to handle divide by zero error in ML

I am new to ML. I need to define a function taking an conditional expression as argument, the problem is if the expression is invalid like "10 div 0 = 0". How can I handle this? For example, the function is defined as following: foo exp1 = if (exp1)…
Jensen
  • 1,653
  • 4
  • 26
  • 42
4
votes
2 answers

Why in Ruby 0.0/0, 3.0/0 and 3/0 behave differently?

If I divide by 0, I get either a ZeroDivisionError, Infinity or NaN depending on what is divided. ruby-1.9.2-p180 :018 > 0.0 / 0 => NaN ruby-1.9.2-p180 :020 > 3.0 / 0 => Infinity ruby-1.9.2-p180 :021 > 3 / 0 ZeroDivisionError: divided by 0 I…
Evgeny Shadchnev
  • 7,320
  • 4
  • 27
  • 30
4
votes
1 answer

Handle "Divide by zero error encountered error"

SELECT ((CASE WHEN (qid2.AgeBelow_16 - qid1.AgeBelow_16)= 0 THEN 1 ELSE (qid2.AgeBelow_16- qid1.AgeBelow_16) END )/ (CASE WHEN [qid1].AgeBelow_16= 0 THEN 1 ELSE [qid1].AgeBelow_16 END))*100 AS AgeBelow_16_Percent, -- otherfields …
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
4
votes
2 answers

How to raise error for nan value in python numpy

I need to automatically raise an error, if I divide by zero in numpy array using the "/" operator, or better if I do any operation, that results in nan. What numpy does instead is to return nan value. I do not need my program to run after that,…
4
votes
1 answer

Firebird computed (calculated) field on server side

Newbie in SQL and development in general, I have a table (COUNTRIES) with fields (INDEX, NAME, POPULATION, AREA) Usually i add a client side (Delphi) Calculated field (DENSITY) and OnCalcField : COUNTRIES.DENSITY=COUNTRIES.POPULATION /…
4
votes
5 answers

How should I tackle ARCTAN (and prevent deviding by zero) without the convenience of ARCTAN2(n,m) being available?

I try to determine the angle from a point (n,m) to (0,0). Without arctan2 being available, I'm running into the problem that m can be 0, which leads to a possible division by zero. What would be an elegant, correct solution to tackling this issue?
Kriem
  • 8,666
  • 16
  • 72
  • 120