Javascript Divide By 0 Error
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 Learn more about hiring developers
Javascript Check Infinity
or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x divide by zero exception in javascript 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
Js Infinity
only takes a minute: Sign up Best way to prevent/handle divide by 0 in javascript up vote 9 down vote favorite 2 What is the best way to prevent divide by 0 in javascript that is accepting user inputs. If javascript check for nan 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 Simply validate your input so entering 0 is impossible might be an option, validation is always a good thing :) –ChrisR Nov 9 javascript errors '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 performant code, I would use if (n === 0 || isNaN(n)) {...}. As it is, I would have forgotten that the NaN case is handled. I know coercion is part of the language, but it's still a bad idea for the most part IMO. Also dividend !== dividend? How about isN
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
Javascript Exceptions
divide any number except zero itself by zero in JavaScript the result will be a 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 http://stackoverflow.com/questions/8072323/best-way-to-prevent-handle-divide-by-0-in-javascript either of these two values 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 http://javascriptexample.net/error07.php value is almost certainly not going to be 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