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

ZeroDivisionError when using sklearn's BaggingClassifier with GridSearchCV

I'm trying to improve a perfectly working Bernoulli Naive Bayes model with bagging. But when I try to cross-validate the BaggingClassifier, I get a very unexpected ZeroDivisionError coming from parallel.py. I've tried to change all the parameters I…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
4
votes
3 answers

c++ google test division by zero

I'm learning to write unit tests, and have started with an easy "Calculator"-class that I wanted to test. I figured out how to use the EXPECT/ASSERT functions, and what test cases etc. are, but I got a problem when I wanted to test the division by…
tinkerbell
  • 229
  • 1
  • 2
  • 9
4
votes
2 answers

Pandas/numpy weighted average ZeroDivisionError

Creating a lambda function to calculate weighted average and sending that to a dictionary. wm = lambda x: np.average(x, weights=df.loc[x.index, 'WEIGHTS']) # Define a dictionary with the functions to apply for a given column: f = {'DRESS_AMT':…
babz
  • 469
  • 6
  • 16
4
votes
2 answers

Division with numpy matrices that might result in nan

How can I divide two numpy matrices A and B in python when sometimes the two matrices will have 0 on the same cell? Basically A[i,j]>=B[i,j] for all i, j. I need to calculate C=A/B. But sometimes A[i,j]==B[i,j]==0. And when this happens I need…
Pietro Speroni
  • 3,131
  • 11
  • 44
  • 55
4
votes
8 answers

Can I ignore a SIGFPE resulting from division by zero?

I have a program which deliberately performs a divide by zero (and stores the result in a volatile variable) in order to halt in certain circumstances. However, I'd like to be able to disable this halting, without changing the macro that performs…
Mikeage
  • 6,424
  • 4
  • 36
  • 54
4
votes
10 answers

Is there a C/C++ function to safely handle division by zero?

We have a situation we want to do a sort of weighted average of two values w1 & w2, based on how far two other values v1 & v2 are away from zero... for example: If v1 is zero, it doesn't get weighted at all so we return w2 If v2 is zero, it doesn't…
Mr. Boy
  • 60,845
  • 93
  • 320
  • 589
4
votes
2 answers

Does a compiler flag exist for gcc (Linux/MinGW) to raise a division by zero error at run time?

I have a complex model written in C++ where denominators sometimes happens to be zeros. I normally check for them, but when I forgot, it's a pain to debug them as the model continues without warnings. Is there a compiler flag, working in both recent…
Antonello
  • 6,092
  • 3
  • 31
  • 56
4
votes
2 answers

32 bit signed integer division gives 0x7fffffff as quotient on PowerPC

I am debugging a production code written in C and its simplest form can be shown as - void test_fun(int sr) { int hr = 0; #define ME 65535 #define SE 256 sr = sr/SE; <-- This should yield 0 if(sr == 1) …
4
votes
3 answers

Java - Cannot Catch ArithmeticException when dividing by zero

I must have done something stupid here. But I can't seem to figure out why this simple code doesn't work. The InputMismatchException works, but the ArithmeticException never get caught. import java.util.InputMismatchException; import…
4
votes
3 answers

Avoid division by zero in C when taking log with respect to a random number

I am currently using C to generate a Gaussian noise. In one step, I need to take the log of a uniformly distributed number u1 = (double) rand() / RAND_MAX. Because u1 may be zero, there is a risk in doing log(u1). So, I need to check. Should I…
Yuxiang Wang
  • 8,095
  • 13
  • 64
  • 95
4
votes
2 answers

Check for division by zero in Func<> call

I am trying to process 2-dimensional arrays with a dynamic mathematical operator. This is the method in question: public float[,] Calculate(int[,] a, int[,] b, Func compute) { int columns = a.GetLength(0); int rows =…
Marco
  • 22,856
  • 9
  • 75
  • 124
4
votes
1 answer

Why 0/0 is NaN and not infinity in Javascript

Division by zero (1/0, 2/0, 3/0, etc) produces infinity. However 0/0 produces NaN. Why?
nicael
  • 18,550
  • 13
  • 57
  • 90
4
votes
3 answers

How to display Infinity to users

I'm designing an interface that displays a table to users with marketing spend and conversions on that marketing spend. The table is something like this: …
Dave Long
  • 9,569
  • 14
  • 59
  • 89
4
votes
2 answers

Linear Programming - variable that equals the sign of an expression

I am trying to write a linear program and need a variable z that equals the sign of x-c, where x is another variable, and c is a constant. I considered z = (x-c)/|x-c|. Unfortunately, if x=c, then this creates division by 0. I cannot use z=x-c,…
user3482751
  • 43
  • 1
  • 4
4
votes
5 answers

number divide by zero is hardware exception

I have learnt during C++ exceptional handling that number divide by zero is a hardware exception. Can anybody explain it why it is called hardware exception
Iqbal Haider
  • 310
  • 1
  • 3
  • 9
Spend Conversions Cost per Conversion