Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Parsing an equation to determine if it will be a division by zero

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 07-04-2012, 07:53 AM PM User | #1 iLochie Regular Coder   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 (8-6)/2 8/2/3 8*6 5/0 etc... How can I parse these? RegExp is ok, if required. Last edited by iLochie; 07-04-2012 at 07:56 AM..
 07-04-2012, 08:33 AM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,481 Thanks: 200 Thanked 2,470 Times in 2,448 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/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: `````` The Dutch have a good record against Holland. - Commentator, ITV1 __________________ 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. Last edited by Philip M; 07-04-2012 at 08:41 AM..
 Users who have thanked Philip M for this post: iLochie (07-04-2012)
07-04-2012, 05:27 PM   PM User | #3
iLochie
Regular Coder

Join Date: Jan 2010
Posts: 101
Thanks: 17
Thanked 5 Times in 5 Posts
Quote:
 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: `````` 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..

07-04-2012, 07:37 PM   PM User | #4
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,481
Thanks: 200
Thanked 2,470 Times in 2,448 Posts
Quote:
 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.
__________________

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.

 07-04-2012, 11:53 PM PM User | #5 felgall Master Coder     Join Date: Sep 2005 Location: Sydney, Australia Posts: 6,094 Thanks: 0 Thanked 579 Times in 569 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/ Beginners need to advise whether they want to learn "Latin" JavaScript for Netscape 3 or "Italian" JavaScript for modern browsers.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 09:36 AM.