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
7
votes
3 answers

Sun Java KeyManagerFactory and null passwords

We are having a problem with the KeyManagerFactory in the Sun JRE 1.6. We are using code similar to the following to upload and use a certificate in p12 format: KeyStore keyStore = KeyStore.getInstance(PKCS12); KeyManagerFactory keyManagerFactory =…
user220668
7
votes
3 answers

Division by zero prevention: Checking the divisor's expression doesn't result in zero vs. checking the divisor isn't zero?

Is division by zero possible in the following case due to the floating point error in the subtraction? float x, y, z; ... if (y != 1.0) z = x / (y - 1.0); In other words, is the following any safer? float divisor = y - 1.0; if (divisor != 0.0) …
6
votes
3 answers

Int vs Double and divide by zero exception

We get compile time error when integer is divided by zero whereas in case of double there is no compilation error but at run-time we get infinity/NaN as the result. Any idea why int & double have different behavior when it comes to divide by zero…
Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
6
votes
3 answers

Check for zero or a denormalized number in c++

I currently have some code where I have to normalize a vector of doubles (divide each element by the sum). When debugging, I see sometimes that the elements in the vector are all 0.0. If I then take the sum of the elements, I get either 0.0 or…
A-A
  • 663
  • 1
  • 13
  • 17
6
votes
9 answers

Divide by Zero: Infinite, NaN, or Zero Division Error?

Why isn't 1/0 == Inf in every language? Is that not the most mathematically correct response? All the languages I'm familiar with are capable of expressing both Infinite and NaN values, so why would they choose to throw an error or return NaN…
Neil Traft
  • 18,367
  • 15
  • 63
  • 70
6
votes
2 answers

remainder of integer division by 0

Consider integer division a = bq + r where a, b, q, r are respectively: dividend, divisor, quotient, and remainder. Particularly when b = 0, there is no unique q that satisfies the equation for a given a, and hence it makes sense that the quotient…
sawa
  • 165,429
  • 45
  • 277
  • 381
6
votes
2 answers

"if" statement in c++ doesn't evaluate conditions from left to right

I was referring to the question ""IF" argument evaluation order?" for understanding the evaluation order for "if" statement in c++. Here is the code where the conditions in if statements are evaluated in a wrong order. #include using…
Ayush Pandey
  • 565
  • 6
  • 19
6
votes
3 answers

How to handle the divide by zero exception in List Comprehensions while dividing 2 lists in Python

How to handle the divide by zero exception in List Comprehensions while dividing 2 lists in Python: From below example: from operator import truediv result_list = map(truediv, [i for i in list1], [j for j in list2]) where the list2 can contain the…
user3270602
  • 696
  • 7
  • 15
6
votes
3 answers

Scala: Divide by zero

I have something like this in my app: def something(x: Int, y: Int) Z { (x / y) } Now, if the someval is not a number (meaning that either x or y is equal to 0), then I'd like Z to just become 0 instead of displaying an error…
goo
  • 2,230
  • 4
  • 32
  • 53
6
votes
5 answers

Division by zero error

I have this code throwing up the error:
Seth-77
  • 254
  • 2
  • 4
  • 15
6
votes
2 answers

force Division by zero default to NaN instead of Inf

I'm wondering if there might be a setting I'm overlooking to force R to return NaN instead of ±Inf when dividing by zero. I too often findmyself doing something like results[is.infinite(results)] <- NaN I'm hoping to skip the filtering/search…
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
5
votes
1 answer

Division by zero error of Solr StatsComponent for date field in case of no results

I have a number of docs indexed by Solr 3.5, which contain date fields (solr.DateField) among others. Now I do request to Solr component which should return no…
dev4
  • 195
  • 1
  • 7
5
votes
2 answers

Is it possible to force Clojure into using primitive types instead of their boxed versions?

I have a following sample of code: (type (apply / [5.0 0])) It throws an unexpected error - "Division By Zero" (expected behavior: return Inf) Probably, it happens due to auto-boxing - can it be prevented?
ischenko
  • 81
  • 1
  • 4
5
votes
1 answer

Divide by zero fault where there is no apparent division in code

The following code is derived from a question here. (I am aware there are several logical problems with the code, but I do not understand how they are causing this problem.) Edit: I am using CLANG on Windows, with common warning displayed for…
ryyker
  • 22,849
  • 3
  • 43
  • 87
5
votes
1 answer

go float zero division compiler error

How does this behaviour sense? wouln't it make mor sense to just print an compiler warning instead of an error? func main() { var y float64 = 0.0 var x float64 = 4.0 / y fmt.Println(x) } +Inf func main() { var x float64 = 4.0 /…
jonathan-dev
  • 330
  • 1
  • 3
  • 16