dividebyzero

Is 1/0 a legal Java expression?

The following compiles fine in my Eclipse: final int j = 1/0; // compiles fine!!! // throws ArithmeticException: / by zero at run-time Java prevents many "dumb code" from even compiling in the first place (e.g. "Five" instanceof Number doesn't compile!), so the fact this didn't even generate as much as a warning was very surprising to...

Can bad stuff happen when dividing 1/a very small float?

If I want to check that positive float A is less than the inverse square of another positive float B (in C99), could something go wrong if B is very small? I could imagine checking it like if(A<1/(B*B)) but if B is small enough, would this possibly result in infinity? If that were to happen, would the code still work correctly in al...

Division by zero: Undefined Behavior or Implementation Defined in C and/or C++ ?

Regarding division by zero, the standards say: C99 6.5.5p5 - The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined. C++03 5.6.4 - The binary / opera...

Why is 0 divided by 0 an error?

I have come across this problem in a calculation I do in my code, where the divisor is 0 if the divident is 0 too. In my code I return 0 for that case. I am wondering, while division by zero is generally undefined, why not make an exception for this case? My understanding why division by zero is undefined is basically that it cannot be r...

SQL Server: How to handle divide by 0 cases in query

Hi all, I have a query that looks like this, and which is getting a percentage: SELECT SUM(convert(decimal(3,2), CASE WHEN Status_ID = 1 AND State_ID = 14 THEN 1 ELSE 0 END))/ SUM(convert(decimal(3,2),CASE WHEN State_ID = 14 THEN 1 ELSE 0 END)) * 100 FROM SomeTable This may return something like 59.77803 in a norm...

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 get weighted at all so we return w1 If both values are equally far from zero, we do a me...

Why is divide by zero a problem?

//////// UPDATE: Christ! I had another look at the program, to see if I was being silly. And indeed I am. The error was in fact not a divide by zero error but 'ArrayIndexOutOfBoundsException:0' This is because because damageTaken is actually an array of values that stores many different 'damages'. So the equation I have is probably corr...

Why does this divide-by-zero error only occur in optimized code?

I just found a bug that, strangely, occurred only when optimization was turned on (g++ -O2). It was an Arithmetic exception in the following code, when interval was set to zero (from a command line argument): for(int i = 0; i < n; ++i) { if((i + 1) % interval == 0) { // exception here DoSomething(); } } It's obvious that modul...

How to cause an intentional division by zero?

For testing reasons I would like to cause a division by zero in my C++ code. I wrote this code: int x = 9; cout << "int x=" << x; int y = 10/(x-9); y += 10; I see "int =9" printed on the screen, but the application doesn't crash. Is it because of some compiler optimizations (I compile with gcc)? What could be the reason? ...