Divide By 0 Error In C
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business
Divide By 0 Error In Sql
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation divide by 0 error java Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Divide By 0 Error Excel
you, helping each other. Join them; it only takes a minute: Sign up divide by zero - c programming up vote 6 down vote favorite I have a question about the next code: int main { double excel hide divide by 0 error x = 0; double y = 0/x; if(y==1) {.....} .... .... return 0; } When I run the code on my computer, I get no runtime error and I see that y = -nan(0x8000000000000). Why it is not a runtime error to divide by zero? Additionally, when I change the first line to int x = 0; now there is a runtime error. What is the difference? c floating-point nan share|improve this question edited Feb how to get rid of divide by 0 error in excel 23 at 10:56 Ville-Valtteri Tiittanen 1,144221 asked Oct 28 '12 at 16:44 wantToLearn 203413 3 @Jens: No, annex F of the C99 standard overrides this and you don't get undefined behavior for floating point. Not all implementations support annex F, but yours and mine do. –Dietrich Epp Oct 28 '12 at 17:00 add a comment| 3 Answers 3 active oldest votes up vote 11 down vote You can't rely on this "working" (i.e. doing the same thing all the time, portably) at all, it's undefined behavior in C for the second case, and also for the first if your implementation doesn't define __STDC_IEC_559__ (this is, I believe, rare these days). C99, §6.5.5/5 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. The fact you're getting a "Not a Number" in one case and and not in the other is that one is done in floating-point arithmetic, where, on your implementation (conforming to IEEE 754 division by zero semantics), 0/0 gives a NaN. In the second case, you're using integer arithmetic – undefined behavior, there's no predicting what will happen. share|improve this answer edited Oct 28 '12 at 17:1
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business
Can't Divide By 0
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation divide by 0 exception java Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Divide By 0 Infinity
you, helping each other. Join them; it only takes a minute: Sign up Division by zero: Undefined Behavior or Implementation Defined in C and/or C++? up vote 16 down vote favorite Regarding division by zero, the standards say: http://stackoverflow.com/questions/13110825/divide-by-zero-c-programming 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 / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. http://stackoverflow.com/questions/3004095/division-by-zero-undefined-behavior-or-implementation-defined-in-c-and-or-c If the second operand of / or % is zero the behavior is undefined. If we were to take the above paragraphs at face value, the answer is clearly Undefined Behavior for both languages. However, if we look further down in the C99 standard we see the following paragraph which appears to be contradictory(1): C99 7.12p4 - The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; Do the standards have some sort of golden rule where Undefined Behavior cannot be superseded by a (potentially) contradictory statement? Barring that, I don't think it's unreasonable to conclude that if your implementation defines the INFINITY macro, division by zero is defined to be such. However, if your implementation does not define such a macro, the behavior is Undefined. I'm curious what the consensus is (if any) on this matter for each of the two languages. Would the answer change if we are talking about integer division int i = 1 / 0 versus floating point division float i = 1.0 / 0.0 ? Note (1) The C++03 standard talks about the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more http://programmers.stackexchange.com/questions/208677/how-to-handle-divide-by-zero-in-a-language-that-doesnt-support-exceptions about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site https://en.wikipedia.org/wiki/Division_by_zero for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers divide by are voted up and rise to the top How to handle divide by zero in a language that doesn't support exceptions? up vote 58 down vote favorite 8 I'm in the middle of developing a new programming language to solve some business requirements, and this language is targeted at novice users. So there is no support for exception handling in the language, and I wouldn't expect them to use divide by 0 it even if I added it. I've reached the point where I have to implement the divide operator, and I'm wondering how to best handle a divide by zero error? I seem to have only three possible ways to handle this case. Ignore the error and produce 0 as the result. Logging a warning if possible. Add NaN as a possible value for numbers, but that raises questions about how to handle NaN values in other areas of the language. Terminate the execution of the program and report to the user a severe error occurred. Option #1 seems the only reasonable solution. Option #3 is not practical as this language will be used to run logic as a nightly cron. What are my alternatives to handling a divide by zero error, and what are the risks with going with option #1. programming-languages error-handling share|improve this question asked Aug 18 '13 at 11:45 ThinkingMedia 9,97833265 12 if you did add exception support and the user didn't catch it then you'd have option #3 –ratchet freak Aug 18 '13 at 11:54 82 I'm curious, what kind of stupid requirement would require you to create a whole new programming language? In my experience, eve
see Division by zero (disambiguation). This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations. Please help to improve this article by introducing more precise citations. (April 2016) (Learn how and when to remove this template message) The function y=1/x. As x approaches 0 from the right, y approaches infinity. As x approaches 0 from the left, y approaches negative infinity. In mathematics, division by zero is division where the divisor (denominator) is zero. Such a division can be formally expressed as a/0 where a is the dividend (numerator). In ordinary arithmetic, the expression has no meaning, as there is no number which, multiplied by 0, gives a (assuming a≠0), and so division by zero is undefined. Since any number multiplied by zero is zero, the expression 0/0 also has no defined value; when it is the form of a limit, it is an indeterminate form. Historically, one of the earliest recorded references to the mathematical impossibility of assigning a value to a/0 is contained in George Berkeley's criticism of infinitesimal calculus in The Analyst ("ghosts of departed quantities").[1] There are mathematical structures in which a/0 is defined for some a such as in the Riemann sphere and the projectively extended real line; however, such structures cannot satisfy every ordinary rule of arithmetic (the field axioms). 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, result in a special not-a-number value, a freeze via infinite loop, or a crash. Contents 1 Elementary arithmetic 1.1 Early attempts 2 Algebra 2.1 Division as the inverse of multiplication 2.2 Fallacies 3 Calculus 3.1 Extended real line 3.1.1 Formal operations 3.2 Projectively extended real