Divide By Zero Error In C
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss divide by zero error sql the workings and policies of this site About Us Learn more
Divide By Zero Error Java
about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow
Divide By Zero Error Encountered
Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping
Excel Divide By Zero Error Hide
each other. Join them; it only takes a minute: Sign up how to check if there is a division by zero in c up vote 6 down vote favorite #include
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and divide by zero error encountered. in sql server policies of this site About Us Learn more about Stack Overflow the divide by zero error encountered in sql server 2012 company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users divide by zero error encountered. the statement has been terminated Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes http://stackoverflow.com/questions/2485547/how-to-check-if-there-is-a-division-by-zero-in-c 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 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). http://stackoverflow.com/questions/13110825/divide-by-zero-c-programming 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 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
C - Basic Syntax C - Data Types C - Variables C - Constants C - Storage Classes C - Operators C - Decision Making C - Loops C - Functions C - Scope Rules C - Arrays C - Pointers C https://www.tutorialspoint.com/cprogramming/c_error_handling.htm - Strings C - Structures C - Unions C - Bit Fields C - Typedef C https://rosettacode.org/wiki/Detect_division_by_zero - Input & Output C - File I/O C - Preprocessors C - Header Files C - Type Casting C - Error Handling C - Recursion C - Variable Arguments C - Memory Management C - Command Line Arguments C Programming Resources C - Questions & Answers C - Quick Guide C - Useful Resources C - Discussion Selected Reading Developer's Best Practices divide by Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who C - Error Handling Advertisements Previous Page Next Page As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno. divide by zero It is set as a global variable and indicates an error occurred during any function call. You can find various error codes defined in
to:navigation, search Detect division by zero You are encouraged to solve this task according to the task description, using any language you may know. Task Write a function to detect a divide by zero error without checking if the denominator is zero. Contents 1 8th 2 ABAP 3 Ada 4 Aime 5 ALGOL 68 6 AutoHotkey 7 BASIC 7.1 Applesoft BASIC 7.2 BBC BASIC 7.3 Liberty BASIC 7.4 Locomotive Basic 7.5 PureBasic 7.6 Run BASIC 7.7 TI-89 BASIC 8 Batch File 9 C 9.1 Library: POSIX 10 C++ 11 C# 12 Ceylon 13 Clojure 14 COBOL 15 Common Lisp 16 D 17 Déjà Vu 18 Delphi 19 E 20 ECL 21 Eiffel 22 Ela 23 Elixir 24 Emacs Lisp 25 Erlang 26 ERRE 27 F# 28 Factor 29 Fancy 30 Forth 31 Fortran 32 FutureBasic 33 Go 34 Groovy 35 Haskell 36 HicEst 37 I 38 IDL 39 Icon and Unicon 40 J 41 Java 42 JavaScript 43 jq 44 Julia 45 LabVIEW 46 Lasso 47 Lingo 48 Lua 49 M4 50 Maple 51 Mathematica / Wolfram Language 52 MATLAB 53 Maxima 54 MAXScript 55 MUMPS 56 mIRC Scripting Language 57 NetLogo 58 NetRexx 59 Nim 60 OCaml 61 Oforth 62 Octave 63 ooRexx 64 Oz 65 PARI/GP 66 Pascal 67 Perl 68 Perl 6 68.1 Using Multi Method Dispatch 69 PHP 70 PicoLisp 71 PL/I 72 PL/SQL 73 PowerShell 74 Pure 75 Python 76 R 77 Racket 78 REBOL 79 REXX 80 Ring 81 Ruby 82 Scala 83 Seed7 84 Sidef 85 Slate 86 Smalltalk 87 SNOBOL4 88 Standard ML 89 Tcl 90 TXR 91 Ursa 92 VBScript 93 XPL0 94 Yorick 95 zkl 8th[edit] Division by zero results in the value "Inf": 1 0 n:/ Inf? . cr Output:true ABAP[edit] report zdiv_zerodata x type i.try. x = 1 / 0.catch CX_SY_ZERODIVIDE. write 'Divide by zero.'.endtry. Ada[edit] -- Divide By Zero Detectionwith Ada.Text_Io; use Ada.Text_Io;with Ada.Float_Text_Io; use Ada.Float_Text_Io;with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;procedure Divide_By_Zero is Fnum: Float:= 1.0; Fdenom: Float:= 0.0; Fresult: Float; Inum: Integer:= 1; Idenom: Integer:= 0; Iresult: Integer;begin begin Put("Integer divide by zero: "); Iresult:= Inum / Idenom; Put(Item => Iresult); exception when Constraint_Error => Put("Division by zero detected."); end; New_Line; Put("Floating point divide by zero: "); Fresult:= Fnum / Fdenom; if Fresult > Float'Last then Put("Division by zero detected (infinite value)."); else Put(Item => Fresult, Aft => 9, Exp => 0); end if; New_Line;end Divide_By_Zero; Output: Integer divide by zero: Division by zero detected. Floating poin