handling division by zero run-time errors: any suggestion?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

handling division by zero run-time errors: any suggestion?

 
0
  #1
Nov 10th, 2008
Hi all there.
I was given the task to write a simple Complex Number class as an exercise for a class I'm taking. I already met my professor and all went well () but I'm left with a question which I forgot to ask her: how should I handle any attempt of division by zero?
So far I managed an - ugly - solution: I declared a static bool variable for the class and set it to false. Then, at any time a division by zero is about to occurr all my class would do is return/process 0+0i as the result and set static bool div_by_zero_attempted to true. Then, in the destructor, I check its state and eventually print on screen an error message which tells that the results given by the program are not reliable. After that I reset the variable to false (just to avoid multiple error messages as long as one is enough for my purpose).
I am providing an extract from my code (the operator/ and the destructor) and I'm asking to you: is there a better/standard way to handle such cases?

Here's the code, anticipated thanks for any reply

  1. Complex Complex::operator/(Complex op2) {
  2. Complex temp;
  3. if(op2.Real==0&&op2.Imaginary==0) {
  4. temp.Real = 0;
  5. temp.Imaginary = 0;
  6. Complex::div_by_zero_attempted = true;
  7. }
  8. else {
  9. temp.Real = (Real*op2.Real) + (Imaginary*op2.Imaginary);
  10. temp.Imaginary = (op2.Real*Imaginary) - (Real*op2.Imaginary);
  11. temp = temp/(op2.Modulus()*op2.Modulus());
  12. }
  13. return temp;
  14. }
  15.  
  16. Complex::~Complex(void) {
  17. if(div_by_zero_attempted) {
  18. std::cout << std::endl << "The programm tried to process a division by zero. Any results should not be trusted." << std::endl;
  19. div_by_zero_attempted = false;
  20. }
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,817
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: handling division by zero run-time errors: any suggestion?

 
1
  #2
Nov 10th, 2008
These links might be helpful.

http://www.deitel.com/articles/cplus...325/index.html
http://www.cis.gsu.edu/~mmoore/CIS32...exceptions.ppt

You need PowerPoint for the second one.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: handling division by zero run-time errors: any suggestion?

 
0
  #3
Nov 10th, 2008
Thank you for the fast reply
I was suspecting that it had to do with exceptions (I only heard of them) but we haven't already covered the topic in class. I'm going to study your links carefully - thank you again!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: handling division by zero run-time errors: any suggestion?

 
0
  #4
Nov 10th, 2008
Imagine your program user's reaction:

Surprise! You waste a time because "the program tried to process a division by zero. Any results should not be trusted."...

Obviously the best design solution is: DO NOTHING. Let the program catch divide by zero exception. It's not a problem of a low-lelel class. That's why exception mechanics was invented.
Last edited by ArkM; Nov 10th, 2008 at 3:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: handling division by zero run-time errors: any suggestion?

 
0
  #5
Nov 10th, 2008
Originally Posted by ArkM View Post
Imagine your program user's reaction:

Surprise! You waste a time because "the program tried to process a division by zero. Any results should not be trusted."
Yeah you're right
I'm just reading the first link VernonDozier provided - I will fix my class as soon as I've finished reading and playing around a little

Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC