Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5

07042012, 06:53 AM #1
 Join Date
 Jan 2010
 Posts
 101
 Thanks
 17
 Thanked 5 Times in 5 Posts
Parsing an equation to determine if it will be a division by zero
I've got a problem on my hands. I've got control of the input to my function, but I don't know what the equation will be. As the numbers that the equation is calculating against are also unknown, there's a possibility of a division by zero. What I'm wondering is if someone knows of a clever way of determining the denominator of an expression before passing it to be evaluated.
For example, the input could be, but is not limited to:
 2*5/4
 (86)/2
 8/2/3
 8*6
 5/0
etc...
How can I parse these? RegExp is ok, if required.Last edited by iLochie; 07042012 at 06:56 AM.
07042012, 07:33 AM
#2
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,598
 Thanks
 207
 Thanked 2,591 Times in 2,569 Posts
I don't think you can detect whether the denominator is zero except by evaluating it as it might be expressed as (5*2/101)
JavaScript does not give an error on division by 0, but you can detect divison by zero resulting in infinity easily enough:
Code:<script type = "text/javascript"> function divByZero(dividend,divisor) { var quotient=dividend/divisor; if(isNaN(quotient)) return 0; // can be changed to whatever is desired by the programmer to be 0, false, or Infinity if (Math.abs(quotient) === Infinity) { alert ("The result is + infinity"); } return quotient; //Will return Infinity or Infinity in cases of, for example, 5/0 or 7/0 respectively } alert(divByZero(5,0)); </script>
The Dutch have a good record against Holland.  Commentator, ITV1
Last edited by Philip M; 07042012 at 07:41 AM.
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Users who have thanked Philip M for this post:
iLochie (07042012)
07042012, 04:27 PM
#3
 Join Date
 Jan 2010
 Posts
 101
 Thanks
 17
 Thanked 5 Times in 5 Posts
07042012, 06:37 PM
#4
 Join Date
 Jun 2002
 Location
 London, England
 Posts
 18,598
 Thanks
 207
 Thanked 2,591 Times in 2,569 Posts
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
07042012, 10:53 PM
#5
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 7,086
 Thanks
 0
 Thanked 701 Times in 690 Posts
If you want to actually throw an error if an attempt is made to divide by zero (as would happen in most other languages) then immediately after the calculation test if the result is Infinity or NaN and if it is then throw a rangeError.
All but one possible divide by zero in JavaScript give Infinity as the result  the one exception is 0/0 which gives NaN instead.
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.