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
15
votes
4 answers

SQL Server Check for IsNull and for Zero

I have the following: set @SomeVariable = @AnotherVariable/isnull(@VariableEqualToZero,1) - 1 If @VariableEqualToZero is null it substitutes the 1. I need it to substitute 1 if @VariableEqualToZero = 0 as well. How do I do this?
Jeff
  • 8,020
  • 34
  • 99
  • 157
14
votes
7 answers

try-catch for division by zero

My question is about try-catch blocks on a simple division by zero example. You see the first line of try? If I cast any of those two variables to the double the program does not recognize the catch block. In my opinion, whether I cast or not only…
Firat
  • 141
  • 1
  • 1
  • 3
13
votes
11 answers

PHP Warning: Division by zero

I'm learning php and built an experimental form-based calculator (also using html & POST method) that returns values to a table. The calculator is functional when I enter my values and click submit, but I keep getting two "Division by zero" errors…
Chip
  • 633
  • 2
  • 6
  • 14
12
votes
8 answers

What is the result of divide by zero?

To be clear, I am not looking for NaN or infinity, or asking what the answer to x/0 should be. What I'm looking for is this: Based on how division is performed in hardware (I do not know how it is done), if division were to be performed with a…
yoozer8
  • 7,361
  • 7
  • 58
  • 93
12
votes
2 answers

Java: Printing out an object for debugging

I would like an easy way to print out a java object, or to say it another way, serialize an object as a string. I would like to see the values of all variables contained within the object, and if there are more objects (like a list or whatever) it…
Muhd
  • 24,305
  • 22
  • 61
  • 78
12
votes
1 answer

Coq QArith division by zero is zero, why?

I noticed that in Coq's definition of rationals the inverse of zero is defined to zero. (Usually, division by zero is not well-defined/legal/allowed.) Require Import QArith. Lemma inv_zero_is_zero: (/ 0) == 0. Proof. unfold Qeq. reflexivity.…
larsr
  • 5,447
  • 19
  • 38
12
votes
3 answers

How to eliminate "divide by 0" error in template code

I'm using a pair of integer template parameters to specify a ratio, since I can't use a double as a template parameter. The conversion into a double is protected against divide-by-zero with a ternary. This worked in an earlier version of the…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
12
votes
4 answers

How to get Python division by -0.0 and 0.0 to result in -Inf and Inf, respectively?

I have a situation where it is reasonable to have a division by 0.0 or by -0.0 where I would expect to see +Inf and -Inf, respectively, as results. It seems that Python enjoys throwing a ZeroDivisionError: float division by zero in either case. …
Chuck
  • 1,850
  • 2
  • 17
  • 28
11
votes
4 answers

TSQL divide by zero encountered despite no columns containing 0

I've been trying to understand why I get a "divide by zero encountered" (Msg 8134) with my SQL query, but I must be missing something. I would like like to know the why for the specific case below, I am not looking for NULLIF, CASE WHEN... or…
Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
11
votes
4 answers

How to avoid DIVIDE BY ZERO error in an SQL query

SELECT YEAR, period, round((1- sum(rej_qty) / sum(recd_qty))*100, 0) FROM TAB_A WHERE sid = '200' AND sdid IN ('4750') AND ( ( YEAR ='2011' AND period IN('01_JAN') ) OR ( YEAR =…
jai
  • 21,519
  • 31
  • 89
  • 120
11
votes
3 answers

Scala division by zero yields different results

I am confused with how Scala handles division by zero. Here is a REPL code snippet. scala> 1/0 java.lang.ArithmeticException: / by zero ... 33 elided scala> 1.toDouble/0.toDouble res1: Double = Infinity scala> 0.0/0.0 res2: Double = NaN scala>…
Ahmadov
  • 1,567
  • 5
  • 31
  • 48
11
votes
2 answers

Can I disable checking for zero division every time the division happens?

In order to better understand Rusts panic/exception mechanisms, I wrote the following piece of code: #![feature(libc)] extern crate libc; fn main() { let mut x: i32; unsafe { x = libc::getchar(); } let y = x - 65; …
Lazarus535
  • 1,158
  • 1
  • 8
  • 23
11
votes
4 answers

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…
FireAphis
  • 6,650
  • 8
  • 42
  • 63
11
votes
2 answers

After division by 0, replace NaN with 0 in numpy arrays

I am dividing two numpy arrays: >>> import numpy as np >>> a1 = np.array([[ 0, 3], [ 0, 2]]) >>> a2 = np.array([[ 0, 3], [ 0, 1]]) >>> d = a1/a2 >>> d array([[ nan, 1.], [ nan, 2.]]) >>>…
Yashu Seth
  • 935
  • 4
  • 9
  • 24
11
votes
11 answers

Divide by zero error, how do I fix this?

C# novice here, when the int 'max' below is 0 I get a divide by zero error, I can see why this happens but how should I handle this when max is 0? position is also an int. private void SetProgressBar(string text, int position, int max) { …
Scott
1 2
3
35 36