Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Parsing an equation to determine if it will be a division by zero

1. ## 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
• (8-6)/2
• 8/2/3
• 8*6
• 5/0

etc...

How can I parse these? RegExp is ok, if required.

• I don't think you can detect whether the denominator is zero except by evaluating it as it might be expressed as (5*2/10-1)

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
}

</script>

The Dutch have a good record against Holland. - Commentator, ITV1

• ## Users who have thanked Philip M for this post:

iLochie (07-04-2012)

• Originally Posted by Philip M
I don't think you can detect whether the denominator is zero except by evaluating it as it might be expressed as (5*2/10-1)

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
}

</script>

The Dutch have a good record against Holland. - Commentator, ITV1
Thanks for the insight Philip, your comments are always welcomed. I'll try what you've recommended. Now I just have to figure out how to obtain any quotients in the expression..

• Originally Posted by iLochie
Thanks for the insight Philip, your comments are always welcomed. I'll try what you've recommended. Now I just have to figure out how to obtain any quotients in the expression..
All you have to do is test your equations for division by zero (result = infinity) before proceding to some further calculation.

• 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.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•