How To Handle Divide By Zero Error In Javascript
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 javascript divide by zero nan the company Business Learn more about hiring developers or posting ads with us Stack
Javascript Check Infinity
Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community divide by 0 javascript of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Best way to prevent/handle divide by 0 in javascript up vote 9 down vote favorite 2 What javascript exceptions 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 insights are much appreciated. javascript divide-by-zero share|improve this question edited Oct 1 '12 at 6:39 Aziz Shaikh 11.6k73954 asked Nov 9 '11 at 21:58 dibs 37711024
Js Infinity
Simply validate your input so entering 0 is impossible might be an option, validation is always a good thing :) –ChrisR Nov 9 '11 at 22:00 2 The "best way" would depend on the computations you're performing and the requirements you have. For instance, is it acceptable for a computation to always succeed but return an incorrect result when fed with invalid input? –Frédéric Hamidi Nov 9 '11 at 22:02 add a comment| 5 Answers 5 active oldest votes up vote 8 down vote accepted There is no way to do that with the normal / and /= operators. The best way to do what you want is with guards: function notZero(n) { n = +n; // Coerce to number. if (!n) { // Matches +0, -0, NaN throw new Error('Invalid dividend ' + n); } return n; } and then do division like numerator / notZero(denominator) Alternatively you can always guard the output function dividend(numerator, denominator) { var dividend = numerator / denominator; if (dividend !== dividend) { throw new Error(numerator + " / " + denominator); } return dividend; } but that loses the readability and expressiveness of /=. share|improve this answer answered Nov 9 '11 at 22:02 Mike Samuel 74.9k16140182 2 Unless you need very performan
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the javascript check for nan workings and policies of this site About Us Learn more about Stack javascript errors Overflow the company Business Learn more about hiring developers or posting ads with us Programmers Questions Tags Users
Division By Zero
Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes http://stackoverflow.com/questions/8072323/best-way-to-prevent-handle-divide-by-0-in-javascript a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers 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 http://programmers.stackexchange.com/questions/208677/how-to-handle-divide-by-zero-in-a-language-that-doesnt-support-exceptions 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 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
to divide by zero. While other languages will crash the program if there is an attempt made to divide by zero, JavaScript does not.If you divide any http://javascriptexample.net/error07.php number except zero itself by zero in JavaScript the result will be a https://www.tutorialspoint.com/javascript/javascript_error_handling.htm special numerical value in JavaScript called Infinity. The only division by zero that doesn't produce that value is if you attempt to divide zero by zero which returns a different value - NaN. JavaScript provides functions so that we can easily test for if either of these two values divide by are returned.What this means is that while other languages need to test for zero before performing the division in order to avoid having the code crash, we can test afterwards in JavaScript in situations where it would make the code more complicated if we were to test before - particularly in complex calculations where the value is almost certainly not going to be divide by zero zero.Of course there may be cases where you don't want the processing to continue if an attempt is made to divide by zero. In this example we will get JavaScript to behave the same way that other browsers do when presented with an attempt to divide by zero and will produce an appropriate error message. JavaScript answer = numer / denom; if (!isFinite(answer)) throw new RangeError('Divide by zero error'); PREVIOUSCATEGORYjsBin This site is © copyright Stephen Chapman - Felgall Pty Ltd 2011-2016. Privacy Policy | Terms and Conditions You are welcome to use any the example JavaScript from this site in the scripts for your site or any that you develop for others but may not use the longer example scripts that contain a copyright notice in any other way without permission. Home Examples The Basics Objects Functions Events BOM DOM Basic DOM DOM Tables DOM Forms Server Interaction RegExp Cookies Error Handling Using Functions Object Oriented Extend Objects Animation Canvas Homework The Bad Bits jQuery The Future Extras Utilities Compressor Formatter View Source Library Objects BOM DOM Events Ajax Extended Date About/Contact Site Map
Syntax Javascript - Enabling Javascript - Placement Javascript - Variables Javascript - Operators Javascript - If...Else Javascript - Switch Case Javascript - While Loop Javascript - For Loop Javascript - For...in Javascript - Loop Control Javascript - Functions Javascript - Events Javascript - Cookies Javascript - Page Redirect Javascript - Dialog Boxes Javascript - Void Keyword Javascript - Page Printing JavaScript Objects Javascript - Objects Javascript - Number Javascript - Boolean Javascript - Strings Javascript - Arrays Javascript - Date Javascript - Math Javascript - RegExp Javascript - HTML DOM JavaScript Advanced Javascript - Error Handling Javascript - Validations Javascript - Animation Javascript - Multimedia Javascript - Debugging Javascript - Image Map Javascript - Browsers IMS DB Resources Javascript - Questions And Answers Javascript - Quick Guide Javascript - Functions Javascript - Resources Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who JavaScript - Errors & Exceptions Handling Advertisements Previous Page Next Page There are three types of errors in programming: (a) Syntax Errors, (b) Runtime Errors, and (c) Logical Errors. Syntax Errors Syntax errors, also called parsing errors, occur at compile time in traditional programming languages and at interpret time in JavaScript. For example, the following line causes a syntax error because it is missing a closing parenthesis. When a syntax error occurs in JavaScript, only the code contained within the same thread as the syntax error is affected and the rest of the code in other threads gets executed assuming nothing in them depends on the code containing t