Thread: Arithmetic exception

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    35

    Arithmetic exception

    Hi The following is my code I am getting Arithmetic exception in gcd function at rem=a%b can anyone help me finf why? how do I fix it?

    Code:
    #include "Pionare.h"
    
    int main(int argc, char * argv[]) {
    int  n=atoi(argv[1]);// convert string to integer
      Pionare Pionare1(n);
       return 0;
    }
    
    Pionare::Pionare(int n)
    {
    cout << "am in cons";
      this->n=n;
    cout << "n is" << n;
     Findab();
    }
    
    void Pionare::Findab(){
    cout << "n is" << n;
      for(b=1;b<=n;b++){
        for(a=1;a<= b*1.414;a++){
    cout << "am here";
         if(Gcd(a,b)==1){
    
    	p=a*a*a*a-6*a*a*b*b+b*b*b*b;
    
    	q=(a*a+b*b)*(a*a+b*b);
    	B=-54*(p*p*p*p*p*p-33*p*p*p*p*q*q-33*p*p*q*q*q*q+q*q*q*q*q*q);
    	A=-27*(p*p*p*p+14*p*p*q*q+q*q*q*q);       
    
    	       //output to file
          }
        }
      }
    }
     
    int Pionare::Gcd(int a,int b){
    int rem;
     cout<<a<<endl<<b<<endl;
    while (a !=0)
    
    {
      rem = a % b;
    
    a= b;
    
    b = rem;
    }
    
    return (a);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    a%b should only cause a fuss if b is 0.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Or perhaps if a or b is negative??

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    void Pionare::Findab(){
    cout << "n is" << n;
      for(b=1;b<=n;b++){
        for(a=1;a<= b*1.414;a++){
    cout << "am here";
         if(Gcd(a,b)==1){
    
    	p=a*a*a*a-6*a*a*b*b+b*b*b*b;
    
    	q=(a*a+b*b)*(a*a+b*b);
    	B=-54*(p*p*p*p*p*p-33*p*p*p*p*q*q-33*p*p*q*q*q*q+q*q*q*q*q*q);
    	A=-27*(p*p*p*p+14*p*p*q*q+q*q*q*q);       
    
    	       //output to file
          }
        }
      }
    }
    How are a and b defined on this scope? Ints? Doubles? In order to leverage your multiplication, they should be doubles (or floats).

    Todd

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Todd Burch View Post
    Or perhaps if a or b is negative??
    Division is still defined for negative numbers. -7%-2 is -1; 7%-2 is 1.

    % is only defined for integers, so a and b are integers. There's not anything wrong with a <= b*1.414; everything will get promoted to double and everyone's happy. I mean, how else are you going to represent sqrt(2) without having it evaluated each time?

    I suppose I should mention that your Gcd function, while interesting, will by necessity always return 0. Try while (b!=0).

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int Pionare::Gcd(int a,int b)
    {
        int rem;
        cout<<a<<endl<<b<<endl;
        while (a !=0)
        {
            rem = a % b;
          
    	a= b;
          
    	b = rem;
        }
        
        return (a);
    }
    Formatted for easier reading...

    If rem becomes zero during the calculation (e.g. a is 6, b is 3), then the next calculation will divide by zero.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  2. exception handling
    By coletek in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2009, 05:28 PM
  3. Handle C++ exception and structured exception together
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2008, 09:21 PM
  4. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  5. Problem with the exception class in MINGW
    By indigo0086 in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2007, 01:12 PM