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

How to handle division by 0 for select statements?

I do this $query4 = "( SELECT count(*) FROM checklist c JOIN task AS t ON t.id = c.task_id AND t.active=1 WHERE c.placement_id = m.id ) / ( …
omega
  • 40,311
  • 81
  • 251
  • 474
4
votes
2 answers

What happens when I divide by zero?

Now I'm not asking about the mathematical answer of dividing by zero, I want to what my computer does when it tries to divide by zero? I can see different answers depending on what level we look at? Looking at a high-level I expect the language…
Lerp
  • 2,957
  • 3
  • 24
  • 43
4
votes
3 answers

Why is 0 mod 0 an error?

If I type: int main() { return 0 % 0; } I get back an error: error C2124: divide or mod by zero What is the reason behind this? Isn't the answer zero?
user541686
  • 205,094
  • 128
  • 528
  • 886
4
votes
2 answers

How to enable SIGFPE signal on division by zero in IOS app?

I develop an app in Xcode 4.5 (llvm 4.1 compiler) for ios 5/6 and use signal and exception handlers to log errors. However I've found that division by zero never raises SIGFPE signal. On linux systems I can use feenableexcept to set traps. But this…
Tertium
  • 6,049
  • 3
  • 30
  • 51
3
votes
5 answers

Calculating percentage changes without dividing by zero

I have the following the PHP that I am using to calculate percentage decreases or increases: function CalculatePercentageIncrease( $nLastMonthPeriod, $nCurrentPeriod ) { if ( !is_numeric( $nLastMonthPeriod ) || !is_numeric( $nCurrentPeriod ) ) …
SameOldNick
  • 2,397
  • 24
  • 33
3
votes
5 answers

Is dividing by zero accompanied with a runtime error ever useful in C++?

According to C++ Standard (5/5) dividing by zero is undefined behavior. Now consider this code (lots of useless statements are there to prevent the compiler from optimizing code out): int main() { char buffer[1] = {}; int len = strlen(…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
3
votes
8 answers

Why dividing an integer by zero and type casting it to float results infinity?

I had already searched through different questions on this topic but not get a clear idea. Check this code: class Test{ public static void main(String[] s){ int a=5; float b=(float)a/0; System.out.print(b); } } the…
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
3
votes
2 answers

@ division by 0 returns fatal in PHP 8

In PHP 7 it returns INF which is okay but in PHP 8 onwards its returning a fatal error. Is there anyway to get PHP 8 return INF too ? I have a script where there are like 100 possible…
anjanesh
  • 3,771
  • 7
  • 44
  • 58
3
votes
4 answers

Break into C# debugger for divide by zero

I'm using C# with the XNA library and I'm getting NaNs cropping up in my Vector3 objects. Is there a way to break into the debugger when the offending calculation happens (e.g. a divide by zero)? Currently the program just continues running. I'm…
Martin Stone
  • 12,682
  • 2
  • 39
  • 53
3
votes
1 answer

Typescript type to prevent division by 0

I am creating a calculation system for training purpose with typescript and I get a typing error during division. Do you have any idea how to solve it? type Variable = { value: number resolve: () => number } type NoZeroVariable = { …
3
votes
3 answers

What is the Smallest Nonzero Number that I can Reliably Generate?

I'm doing some graphing-type stuff, and I frequently want to take the slope between two points as dy / dx. However, if my dx is exactly zero, I will get a divide-by-zero error. If dx is zero, I can set it to something small, say 0.001. However, I…
Colin Hancey
  • 219
  • 2
  • 8
3
votes
2 answers

what is meant by 'Most C system provide for logically infinite floating values'?

Initially I declared variables x and y as type int: #include int main(){ int x, y = 0 ; x = 1 / y; printf("%d", x); return 0; } Program crashed (for obvious reasons). Now I declared…
PO_
  • 91
  • 8
3
votes
1 answer

FORTRAN 77 Divide By Zero Behavior

I am working on re-engineering an old FORTRAN77 program to Python for a while now. I'm running into an issue, though: when dividing by zero, it appears that the FORTRAN program just continues processing the data without issue. However, predictably…
Dakota B
  • 138
  • 9
3
votes
1 answer

What 28 frames are elided when dividing by zero in the REPL?

scala> 5 / 0 java.lang.ArithmeticException: / by zero ... 28 elided Twenty eight frames elided for a simple arithmetic expression?! What are these frames, why does Scala need that many to do safe division, and why are they being elided in the…
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
3
votes
2 answers

Is it possible to detect inconsistent equations in Z3, or before passing to Z3?

I was working with z3 with the following example. f=Function('f',IntSort(),IntSort()) n=Int('n') c=Int('c') s=Solver() s.add(c>=0) s.add(f(0)==0) s.add(ForAll([n],Implies(n>=0, f(n+1)==f(n)+10/(n-c)))) The last equation is inconsistent (since n=c…
Tom
  • 904
  • 1
  • 7
  • 21