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
43
votes
5 answers

Dividing by zero in a constant expression

My toy compiler crashes if I divide by zero in a constant expression: int x = 1 / 0; Is this behaviour allowed by the C and/or C++ standards?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
43
votes
8 answers

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…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
42
votes
16 answers

PHP, How to catch a division by zero?

I have a large mathematical expression that has to be created dynamically. For example, once I have parsed "something" the result will be a string like: "$foo+$bar/$baz";. So, for calculating the result of that expression I'm using the eval…
Cristian
  • 198,401
  • 62
  • 356
  • 264
39
votes
11 answers

Best way to prevent/handle divide by 0 in javascript

What is the best way to prevent divide by 0 in javascript that is accepting user inputs. If there is no particular way to achieve this what would be the best way to handle such a situation so as to not prevent other scripts from executing? Any…
dibs
  • 1,018
  • 2
  • 23
  • 35
32
votes
8 answers

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…
SiegeX
  • 135,741
  • 24
  • 144
  • 154
29
votes
5 answers

How should I throw a divide by zero exception in Java without actually dividing by zero?

I have an I2C device that wants two inputs: a denominator and a numerator. Both are written to separate addresses, so no actual calculation (numerator/denominator) is done. The problem with this is that a divide by zero could occur on the I2C…
Eric
  • 95,302
  • 53
  • 242
  • 374
25
votes
5 answers

c++ division by 0

I am running long simulations. I record the results into a vector to compute statistics about the data. I realized that, in theory, those samples could be the result of a division by zero; this is only theoretical, I am pretty sure it's not the…
Bob
  • 10,741
  • 27
  • 89
  • 143
24
votes
4 answers

Division by zero in Haskell

I found a quite strange inconsistency between the behaviour of div and /. *ghci> :t 1 `div` 0 1 `div` 0 :: Integral a => a *ghci> :t 1 / 0 1 / 0 :: Fractional a => a *ghci> 1 / 0 Infinity *ghci> 1 `div` 0 *** Exception: divide by zero I was quite…
Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
24
votes
8 answers

Ruby way: catch division by zero

I have the following method to compute an average: def compute_average(a,b,c,d,e) total = [a,b,c,d,e].sum.to_f average = [a, 2*b, 3*c, 4*d, 5*e].sum / total average.round(2) end It's nothing special, but it has a problem that I expect all…
Andrew
  • 42,517
  • 51
  • 181
  • 281
20
votes
18 answers

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…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
19
votes
4 answers

How does Java handle division by zero?

Does it simply check if divisor is different from zero every time there is division done (even in JIT-ed code)? I mean how VM manages to throw an exception without being previously killed by the OS?
zduny
  • 2,481
  • 1
  • 27
  • 49
18
votes
3 answers

On which platforms does integer divide by zero trigger a floating point exception?

In another question, someone was wondering why they were getting a "floating point error" when in fact they had an integer divide-by-zero in their C++ program. A discussion arose around this, with some asserting that floating point exceptions are in…
Crashworks
  • 40,496
  • 12
  • 101
  • 170
18
votes
4 answers

Why does integer division by zero result in a floating point exception?

Division by zero in a C program results in abnormal termination with the error message Floating point exception (core dumped). This is unsurprising for floating point division, but why does it say this when integer division by zero occurs? Does…
Taymon
  • 24,950
  • 9
  • 62
  • 84
17
votes
1 answer

A Double divided by zero is returning a Divide by Zero error

I am experiencing an unexpected behaviour and was hoping someone could help with some guidance as to what areas to focus an investigation on. I have two methods, one essentially performs a divide by zero test on a double, the second calls an extern…
Kynth
  • 2,597
  • 1
  • 18
  • 24
16
votes
2 answers

Why does this code divide by zero?

I have a small Haskell program, and am curious why a divide by zero exception gets thrown when I run it (GHC 7.0.3) import qualified Data.ByteString.Lazy as B import Codec.Utils convert :: B.ByteString -> [Octet] convert bs = map (head .…
Litherum
  • 22,564
  • 3
  • 23
  • 27
1
2
3
35 36